我正在尝试生成一个点图并为两个独立的分组变量添加平均值和95%CI(因此我总共有2 x 2个组)。
这是我的数据:
GluNorm Gender Treatment
1.317 Male NAC
1.278 Male SAL
1.302 Male SAL
1.376 Male NAC
1.279 Male NAC
1.308 Male SAL
1.451 Male NAC
1.244 Male NAC
1.411 Male SAL
1.16 Male NAC
1.159 Male NAC
1.42 Male SAL
1.407 Male SAL
1.62 Male SAL
1.167 Male SAL
1.377 Male NAC
1.393 Female SAL
1.203 Female NAC
1.191 Female NAC
1.132 Female SAL
1.191 Female SAL
1.589 Female SAL
1.169 Female NAC
1.155 Female SAL
1.249 Female NAC
1.401 Female NAC
1.455 Female SAL
1.481 Female SAL
1.293 Female NAC
1.332 Female NAC
1.462 Female SAL
这就是我想要的情节:
我做了以下尝试:
g<- ggplot(Data, aes(x = Gender, y = GluNorm, fill=Group))+
geom_dotplot(binaxis='y', stackdir='center',stackratio=1.2, dotsize=1.2, binwidth=0.02, position=position_dodge(0.8))
#function that outputs mean, lower limit and upper limit of 95% CI
data_summary <- function(x) {
m <- mean(x, na.rm=TRUE)
sem <-sd(x, na.rm=TRUE)/sqrt(sum(!is.na(x)))
ymin<-m-1.96*sem
ymax<-m+1.96*sem
return(c(y=m,ymin=ymin,ymax=ymax))
}
g + stat_summary(fun.data=data_summary, color="red")
得到以下结果:
有没有人可以建议如何为每个性别x治疗组获得单独的点和条?
谢谢!
答案 0 :(得分:3)
如果您以不同的数量躲避多重点和置信区间,图表将更容易阅读,以便不重叠:
pd1 = position_dodge(0.2)
pd2 = position_dodge(0.65)
ggplot(Data, aes(x = Gender, y = GluNorm, fill=Treatment, color=Treatment))+
geom_dotplot(binaxis='y', stackdir='center',stackratio=1.2,
dotsize=0.8, binwidth=0.02, position=pd2) +
stat_summary(fun.data=data_summary, position=pd1, geom="errorbar", width=0.05) +
stat_summary(fun.data=data_summary, position=pd1, geom="point", size=2) +
scale_fill_manual(values=hcl(c(15,195), 100, 60)) +
scale_color_manual(values=hcl(c(15,195), 50, 40)) +
theme_bw()
您也可以使用内置函数代替data_summary
函数。下面我们使用bootstrapping获得95%的CI:
ggplot(Data, aes(x = Gender, y = GluNorm, fill=Treatment, color=Treatment))+
geom_dotplot(binaxis='y', stackdir='center',stackratio=1.2,
dotsize=0.8, binwidth=0.02, position=pd2) +
stat_summary(fun.data=mean_cl_boot, position=pd1, geom="errorbar", width=0.05) +
stat_summary(fun.y=mean, position=pd1, geom="point", size=2) +
scale_fill_manual(values=hcl(c(15,195), 100, 60)) +
scale_color_manual(values=hcl(c(15,195), 50, 40)) +
theme_bw()
您可以直接绘制点,而不是点图,添加抖动以避免重叠:
ggplot(Data, aes(x = Gender, y = GluNorm, fill=Treatment, color=Treatment))+
geom_point(position=position_jitterdodge(dodge.width=0.65, jitter.height=0, jitter.width=0.25),
alpha=0.7) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.05, position=pd1) +
stat_summary(fun.y=mean, geom="point", size=2, position=pd1) +
scale_fill_manual(values=hcl(c(15,195), 100, 60)) +
scale_color_manual(values=hcl(c(15,195), 50, 40)) +
theme_bw()
或者也许是一个箱子图:
pd1 = position_dodge(0.4)
ggplot(Data, aes(x = Gender, y = GluNorm, fill=Treatment, color=Treatment))+
geom_boxplot(position=pd1, width=0.3, alpha=0.2, color="#00000080",
lwd=0.4, fatten=1.5) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1, position=pd1, size=0.7) +
stat_summary(fun.y=mean, geom="point", size=2, position=pd1) +
scale_fill_manual(values=hcl(c(15,195), 100, 80)) +
scale_color_manual(values=hcl(c(15,195), 50, 30)) +
theme_bw()