R - 绘图子组

时间:2014-05-20 21:14:15

标签: r statistics

我很难弄清楚如何在R中绘制一个子组,随着时间的推移。数据如下。基本上,我想描绘咖啡因的效果如何随着时间的推移改变我的测试表现,但我希望白天将它们分组。

我进行了第一次测试(总是在咖啡因之前),以确定当天的表现,然后看看我的分数在第二次测试中是什么样的,有时是咖啡因,有些则不是。如果我服用咖啡因,它会在后咖啡因变量上标记"yes"

那么,在同一天对变量进行分组的最佳方法是什么,但允许在一天中的时间和是否在咖啡因之后出现差异?

它认为它会像

x <- group_by (DATA, Post Caffaine)
ggplot(aes(x, score) data)

我可以制作个别变量,以便在同一天内更改测试分数,但我认为在对象中更好。我很难过。感谢您的帮助,我希望这很清楚。

Post Caffeine   Score     Time/Date
yes        10   3/17/2014 17:58:28
no          9   3/17/2014 23:55:47
no          7   3/18/2014 18:50:50
no         10   3/18/2014 23:09:03

1 个答案:

答案 0 :(得分:0)

library(lubridate)

# For illustration, I've added a few additional observations plus a new 
# grouping variable
dat = read.table(text="
                 Post_Caffeine   Score     Date Time Pairs
                 yes        10   3/17/2014 18:25:28   1
                 yes        9   3/17/2014 17:38:28   1
                 yes        8   3/17/2014 12:58:28    2
                 yes        9   3/17/2014 09:58:28    3
                 no          9   3/17/2014 16:55:47   1
                 no          9   3/17/2014 15:25:47   1
                 no          7   3/17/2014 11:55:47   2
                 no          8   3/17/2014 7:55:47    3
                 no          7   3/18/2014 18:50:50   4
                 no         8   3/18/2014 23:09:03    5
                 yes          8   3/18/2014 19:50:50  4
                 yes         10   3/18/2014 24:09:03  5", header=TRUE)

dat$date_time = mdy_hms(paste(dat$Date, dat$Time)) 
dat$Date = mdy(dat$Date)
dat$Time = hms(dat$Time)

# This will plot each individual measurement and give you separate lines 
# for Post_Caffeine="yes" and Post_Caffeine="no"
ggplot(dat, aes(hour(Time)+minute(Time)/60, Score, colour=Post_Caffeine)) + 
  geom_point() + geom_line() + 
  scale_y_continuous(limits=c(0,10)) + 
  facet_wrap(~ Date) +
  xlab("Time (24-hour clock)")

# When grouping by the new "Pairs" variable, you get a separate set of points/lines 
# for each value of Pairs. I originally took each "pair" to be one pre- and one 
# post-test, but of course there can be several of each for a given value of 
# Pairs, as the graph shows
ggplot(dat, aes(hour(Time)+minute(Time)/60, Score, 
                group=Pairs, colour=Post_Caffeine)) + 
  geom_line(colour=colors()[76]) +
  geom_point(size=2.5) +  
  scale_y_continuous(limits=c(0,10)) +
  facet_wrap(~ Date) +
  xlab("Time (24-hour clock)")

这两个图分别是这样的: enter image description here