R SD错误条不正确
嗨,我对图中的错误条有问题。这是我的代码R SD错误条不正确,r,ggplot2,R,Ggplot2,嗨,我对图中的错误条有问题。这是我的代码 library(ggplot2) val1 <- sample(1:18) val2 <- sample(20:50, 18) mylet<-c("A", "B", "C") time <-rep(mylet,times=6) id<-rep(c("WT", "KO"), each=9) x1 <- data.frame(id, time, val1, val2) x1$id=factor(x1$id, c("WT",
library(ggplot2)
val1 <- sample(1:18)
val2 <- sample(20:50, 18)
mylet<-c("A", "B", "C")
time <-rep(mylet,times=6)
id<-rep(c("WT", "KO"), each=9)
x1 <- data.frame(id, time, val1, val2)
x1$id=factor(x1$id, c("WT","KO"))
cols = c(3,4)
df1 = transform(x1, mean=rowMeans(x1[cols]), sd=apply(x1[cols],1, sd))
p<-ggplot(data=df1, aes(x=as.factor(time), y=mean, fill=id)) +
geom_bar(position=position_dodge(), stat="identity", colour="black") +
geom_errorbar(aes(ymin=mean-sd, ymax= mean+sd), width=0.2, position=position_dodge(0.9))
p
库(ggplot2)
val1问题在于,在变量df1
中,每个(id
,time
)组合都有多个测量值(例如,对于id WT和time A,平均值分别为13.0、30.5和15.0)。您需要决定如何处理这一级别的复制,是在图中表示它,还是显示它的统计信息。既然你知道实验设计的细节,而且是你的数据,你应该是做出判断的人
例如,对每个(id
,time
)组合的多个点进行平均:
library(plyr)
df2 <- ddply(df1, c("id", "time"), summarize, mean.overall = mean(mean), sd.overall = sd(mean))
库(plyr)
df2问题/问题是什么?我有太多的标准偏差点,一个错误条低于0,第二个红色条有两个错误条。看起来你缺少了一个层次。对于id
和time
的每个组合,您有三行代码,并且您的绘图显示了每个组合的三个错误条。更大的问题是为什么每个组合有三个标准差?它们有什么不同?是的,我刚刚意识到,情节是正确的,val1和val2平均需要18次。谢谢这正是我想做的-谢谢
p <-
ggplot(data = df2, aes(x = as.factor(time), y = mean.overall, fill = id)) +
geom_bar(position = position_dodge(), stat = "identity", colour = "black") +
geom_errorbar(aes(ymin = mean.overall-sd.overall, ymax = mean.overall + sd.overall), width = 0.2, position = position_dodge(0.9))
p