Reputation: 10862
Here's my data:
Assignment final first resub
44 PA07 59.00 48.5 69.5
46 PA07 74.25 72.5 76.0
63 PA07 98.50 98.5 NA
36 PA07 76.25 71.0 81.5
32 PA07 84.50 77.5 91.5
I would like to make a scatter plot with ggplot where the X axis is the value in "first". There are two sets of dots, one set uses the value in final as the y, and the other uses the value in resub in y.
I tried this with the above layout as well as with this layout, and still cannot figure out how to do it.
Assignment Stage Grade
719 PA06 final 78.50
860 PA06 resub NA
1866 Exam final 53.24
665 PA05 resub NA
1881 Exam final 99.34
269 PA03 final 66.00
1675 PA10 resub NA
761 PA06 final 98.50
498 PA04 resub NA
435 PA04 first 100.00
So now my code looks like this, but I think I can refactor it down but not sure how:
gxg = gather(grades, key, Grade, -email)
gxg = separate(gxg, key, c("Assignment", "Stage"))
gxg = spread(gxg, Stage, Grade)
gxg = gather(gxg, key=Stage, value=Grade,-c(first,Assignment, email))
(btw starting str() is:)
'data.frame': 65 obs. of 30 variables:
$ email : chr ...
$ course.final: num 86.3 68.9 38.1 77.4 90 ...
$ part.final : num 78.2 71.5 54.2 98.2 99.8 ...
$ PA01.final : num 78 78 86.5 72.5 86.5 79.5 83 72 90 86 ...
$ PA02.final : num 88.5 93 93 93 91.5 90 93 93 86 85.5 ...
$ PA03.final : num 90 28 81.5 70 77 97 69 83 66 53 ...
$ PA04.final : num 88.8 55.2 0 70.2 73 ...
$ PA04.first : num 83.5 53.5 0 65 73 87 62.5 88 45 30 ...
$ PA04.resub : num 94 57 0 75.5 NA 94 83.5 NA NA 41.5 ...
$ PA05.final : num 96.5 81 0 87.8 96.5 ...
$ PA05.first : num 96.5 81 0 79 96.5 81 96.5 93.5 95 50.5 ...
$ PA05.resub : num NA NA NA 96.5 NA 98.5 NA NA NA NA ...
$ PA06.final : num 98.5 82.5 0 78.5 95 98.5 95 95 100 83 ...
$ PA06.first : num 98.5 82.5 0 78.5 95 98.5 95 95 100 83 ...
$ PA06.resub : num NA NA NA NA NA NA NA NA NA NA ...
$ PA07.final : num 97 63.5 0 63.5 86.2 ...
$ PA07.first : num 97 63.5 0 60 74 84.5 67 81 91.5 88 ...
$ PA07.resub : num NA NA NA 67 98.5 95 NA 98.5 95 NA ...
$ PA08.final : num 100 95 0 97.5 95 97.5 90 95 97.5 95 ...
$ PA08.first : num 100 95 0 97.5 95 97.5 90 95 97.5 95 ...
$ PA08.resub : num NA NA NA NA NA NA NA NA NA NA ...
$ PA09.final : num 98.5 33 0 69 64 98.5 74.5 81.5 100 93 ...
$ PA09.first : num 98.5 33 0 69 81.5 98.5 74.5 67.5 100 93 ...
$ PA09.resub : num NA 33 NA NA 46.5 NA NA 95.5 NA 93 ...
$ PA10.final : num 90 90 NA 98 96 100 90 87 93 97 ...
$ PA10.first : num 90 90 NA 98 96 100 90 87 93 97 ...
$ PA10.resub : logi NA NA NA NA NA NA ...
$ PA11.final : num 45.2 71.5 NA 34.5 91 ...
$ EC.final : num 78 15 20 26 10 155 30 90 47 30 ...
$ Exam.final : num 66.4 57.2 NA 63.3 87.5 ...
Upvotes: 0
Views: 36
Reputation: 18425
The following line in tidyr
will convert your first df to the correct long form.
You can then send this to ggplot
df2 <- df %>% gather(key=Stage,value=Grade,-c(first,Assignment))
ggplot(df2,aes(x=first,y=Grade,colour=Stage)) + geom_point()
In response to follow-up, the following might work...
gxg <- grades %>%
gather(key=key,value=Grade,-email) %>% #convert to long format
separate(key,c(Assignment,Stage)) %>% #separate assignment and stage
spread(key=Stage,value=Grade) %>% #convert Stage back to wide format (as it includes first as well as final and resub)
gather(key=Stage,value=Grade,c(final,resub)) #convert final and resub to long format leaving first intact
Hopefully this will result in gxg
having columns email, Assignment, first, Stage, Grade
...then ggplot(gxg...)
as before.
Upvotes: 1