Bio*_*der 0 plot r mean ggplot2 confidence-interval
我的数据看起来像这样:
A B C
8 5 2
9 3 1
1 2 3
3 1 2
4 3 1
Run Code Online (Sandbox Code Playgroud)
我需要使用ggplot2绘制每个方法的均值以及置信区间.我还想从数据迭代中获得置信区间(例如,使用stat_summary(fun.data = mean_cl),但是我不知道如何绘制这种格式的数据的方法.
我尝试了以下代码,但它没有运行.我不确定第2行需要进入什么.
pd <- position_dodge(0.78)
ggplot(dat, y = c(dat$A,dat$B,dat$C) + ylim(0,10) + theme_bw()) +
stat_summary(geom="bar", fun.y=mean, position = "dodge") +
stat_summary(geom="errorbar", fun.data=mean_cl_normal, position = pd)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Warning messages:
1: Computation failed in `stat_summary()`:
object 'x' not found
2: Computation failed in `stat_summary()`:
object 'x' not found
Run Code Online (Sandbox Code Playgroud)
小智 6
您的数据不是长格式,这意味着它应如下所示:
thing<-data.frame(Group=factor(rep(c("A","B","C"),5)),
Y = c(8,9,1,3,4,
5,3,2,1,3,
2,1,3,2,1)
)
Run Code Online (Sandbox Code Playgroud)
您可以使用类似的功能melt()来帮助获取reshape2包中的数据格式.
一旦你的,你也得(前手工计算数据的手段和SE ggplot或通过正确的表达式中stat_summary的ggplot).您可能已从示例中复制/粘贴,因为您正在使用的函数(例如mean_cl_normal)可能未定义.
那我们手工做吧.
library(plyr)
cdata <- ddply(thing, "Group", summarise,
N = length(Y),
mean = mean(Y),
sd = sd(Y),
se = sd / sqrt(N)
)
cdata
#Group N mean sd se
#1 A 5 4.0 2.236068 1.000000
#2 B 5 3.8 3.033150 1.356466
#3 C 5 1.8 1.788854 0.800000
Run Code Online (Sandbox Code Playgroud)
现在你可以使用了ggplot.
pd <- position_dodge(0.78)
ggplot(cdata, aes(x=Group, y = mean, group = Group)) +
#draws the means
geom_point(position=pd) +
#draws the CI error bars
geom_errorbar(data=cdata, aes(ymin=mean-2*se, ymax=mean+2*se,
color=Group), width=.1, position=pd)
Run Code Online (Sandbox Code Playgroud)
这给出了附图.