I have a dataframe with 2 cond, and 2 time points within each cond (pre and post). I am looking to create a line graph showing the pre and post MEDIAN values from the sc column from a group of subjects on both conds. I would also like there to be a line connecting the pre and post values on each cond. ie. Pre ------ Post for cond #1, and Pre ----- Post for cond #2.
Data:
convert
This is just a sample of the data. There are 11 subjects (id 1:11) in my data set for each condition at each time point. This is the best code I have so far:
id cond time sc
1 1 0 400
2 1 0 370
1 1 1 300
2 1 1 302
1 2 0 402
2 2 0 380
1 2 1 220
2 2 1 203
Right now this code gives me all the points from each condition (cond) and not just one point showing the median values. Been searching online for quite a few hours today but can't find an example similar to what I'm trying to create.
答案 0 :(得分:2)
喜欢这个?
d1 <- "id cond time sc
1 1 0 400
2 1 0 370
1 1 1 300
2 1 1 302
1 2 0 402
2 2 0 380
1 2 1 220
2 2 1 203"
library("data.table")
d1 <- data.table(read.table(text=d1, header = TRUE))
d1[, "median" := median(sc), by=list(cond, time)]
library("ggplot2")
ggplot(d1, aes(x=time, y=median, color=factor(cond))) +
geom_line() +
geom_point()
,并提供:
我发现data.table
是这类“分组”/“汇总”问题的最简单方法,但还有很多其他问题。
另外,请注意调用df
,因为这会覆盖stats::df()
。
答案 1 :(得分:0)
在vanilla R中,您可以使用aggregate
获得与@dardisco的data.tables
解决方案相同的功能。
d1 <- ... as above ...
d2 <- read.table(text=d1, header=T)
d3 <- aggregate(sc ~ time + cond, d2, median)
require(ggplot2)
ggplot(d3) + geom_line(aes(x=time, y=sc, color=factor(cond))