简体   繁体   English

ggplot2 / R中条形图的偏移置信区间

[英]offsetting confidence interval for stripchart in ggplot2/R

I'm trying to create a stripchart/dotplot with a 95% confidence interval using ggplot2 using the data posted below. 我正在尝试使用ggplot2使用下面发布的数据创建具有95%置信区间的带状图/点图。 I want to offset the CIs so they don't overlap the data, since it's hard to interpret them otherwise. 我想抵消配置项,以免它们与数据重叠,因为很难以其他方式解释它们。 I'm using the following code, but setting position_dodge to any number offsets the entire data set (points and CIs) rather than just the CIs. 我正在使用以下代码,但是将position_dodge设置为任何数字都将偏移整个数据集(点和CI),而不仅仅是CI。 Any help would be appreciated. 任何帮助,将不胜感激。

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

Here's the dataset: 这是数据集:

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

I ended up figuring out an answer, but it's not particularly elegant. 我最终想出了一个答案,但这并不是特别优雅。

I converted the groups to numeric (1, 2, 3) and the x-axis to continuous, then defined the breaks (tick marks) as (1, 2, 3) and defined the break labels as "Ctrl, "Hi", and "Lo", thereby replacing the ticks labels with the group names. 我将组转换为数字(1、2、3),将x轴转换为连续的,然后将中断(刻度线)定义为(1、2、3),并将中断标签定义为“ Ctrl,“ Hi”,和“ Lo”,从而用组名替换对勾标签。

Since the x-axis was then continuous, I could offset the data points -0.2 from the tick mark and offset the CI bar +0.2 from the tick mark, giving the separation I was looking for (see code below). 由于x轴是连续的,因此我可以将数据点从刻度线偏移-0.2,并将CI条从刻度线偏移+0.2,从而得到我一直在寻找的间隔(请参见下面的代码)。

A more elegant way to do this would still be appreciated. 仍然可以用一种更优雅的方式来做到这一点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM