这是我的数据:
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
我想用ggplot制作散点图,其中X轴是"第一个"中的值。有两组点,一组使用final中的值作为y,另一组使用res中的值作为y。
我尝试使用上面的布局以及这种布局,但仍然无法弄清楚如何做到这一点。
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
所以现在我的代码看起来像这样,但我想我可以重构它但不确定如何:
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))
(顺便说一句,启动str()是:)
'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 ...
答案 0 :(得分:1)
tidyr
中的以下行会将您的第一个df转换为正确的长格式。
然后,您可以将其发送到ggplot
df2 <- df %>% gather(key=Stage,value=Grade,-c(first,Assignment))
ggplot(df2,aes(x=first,y=Grade,colour=Stage)) + geom_point()
作为对后续行动的回应,以下内容可能有效......
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
希望这会导致gxg
列email, Assignment, first, Stage, Grade
...然后像以前一样ggplot(gxg...)
。