抵消ggplot2 / R中条带图的置信区间

时间:2015-07-30 18:40:29

标签: r ggplot2

我尝试使用ggplot2使用下面发布的数据创建95%置信区间的条带图/点图。我想要抵消CI,以便它们不会重叠数据,因为否则很难解释它们。我使用以下代码,但将position_dodge设置为任何数字都会抵消整个数据集(点和CI),而不仅仅是CI。任何帮助将不胜感激。

dat$Index <- as.numeric(dat$Index)
dat$Group <- as.factor(dat$Group)

dodge <- position=position_dodge(2)

p <- ggplot(data=dat, aes(x=Group, y=Index, fill=Group, color=Group)) +
      geom_point(position=position_jitter(0.1), cex=3) +
      theme_classic() + expand_limits(y=c(-1.0, 1.0)) + 
      stat_summary(fun.data = mean_cl_normal, geom="errorbar", width=0, position=dodge) +
      stat_summary(fun.y = mean, fun.ymin = mean, fun.ymax=mean, geom="point", position=dodge) +  
      theme(legend.position="none") + 
      geom_abline(intercept=0, slope=0, linetype="dotted") + 
      geom_abline(intercept=1, slope=0, linetype="dotted") + 
      geom_abline(intercept=-1, slope=0, linetype="dotted")

p

这是数据集:

Group,Index
Ctrl,0.082
Ctrl,0.085
Ctrl,0.178
Ctrl,0.111
Ctrl,0.386
Ctrl,0.207
Hi,-0.033
Hi,0.473
Hi,0.162
Hi,-0.064
Hi,0.072
Hi,-0.027
Hi,0.166
Hi,0.007
Hi,0.048
Hi,0.091
Lo,0.145
Lo,0.104
Lo,0.128
Lo,0.032
Lo,-0.059
Lo,0.062
Lo,0.082
Lo,0.101

1 个答案:

答案 0 :(得分:0)

我最终弄清楚了答案,但它并不是特别优雅。

我将组转换为数字(1,2,3)并将x轴转换为连续,然后将断点(刻度线)定义为(1,2,3)并将断点标签定义为&#34;按Ctrl,&#34;嗨&#34;和&#34; Lo&#34;,从而用组名替换刻度标签。

由于x轴是连续的,我可以将数据点-0.2与刻度线偏移,并将CI栏+0.2从刻度线上移开,得到我正在寻找的分离(见下面的代码)。 / p>

更优雅的方式仍然可以理解。

dat <- read.csv("ymazedi.csv", header=TRUE)dat$Index <- as.numeric(dat$Index)
dat$Group <- as.numeric(dat$Group)
breaks <- as.numeric(c(1,2,3))
labels <- c("Ctrl", "Hi", "Lo")

p <- ggplot(data=dat, aes(x=Group, y=Index, group=Code, color=Group)) +
      geom_point(aes(x=Group-0.2), position=position_jitter(.1), cex=3) +
      theme_classic() + expand_limits(y=c(-1.0, 1.0), x=c(0.5,3.5)) + 
      scale_x_continuous(breaks=breaks, labels=labels) +
      stat_summary(aes(x=Group+0.2), fun.data = mean_cl_normal, geom="pointrange", width=0) +
      theme(legend.position="none", text=element_text(size=20)) + 
      geom_abline(intercept=0, slope=0, linetype="dotted") + 
      geom_abline(intercept=1, slope=0, linetype="dotted") + 
      geom_abline(intercept=-1, slope=0, linetype="dotted")

p