geom_lines没有将它们应该与ggplot中的误差条连接起来

时间:2013-12-05 06:58:53

标签: r ggplot2

我准备好以下数据集来绘制误差线和线图

> growth
   treatment       class variable  N       value          sd          se          ci
1   elevated    Dominant RBAI2012 18 0.014127713 0.009739951 0.002295728 0.004843564
2   elevated    Dominant RBAI2013 18 0.021869978 0.013578741 0.003200540 0.006752549
3   elevated  Codominant RBAI2012 40 0.011564725 0.013718591 0.002169100 0.004387418
4   elevated  Codominant RBAI2013 41 0.011471512 0.011091167 0.001732149 0.003500804
5   elevated Subordinate RBAI2012 24 0.004419784 0.009286883 0.001895677 0.003921507
6   elevated Subordinate RBAI2013 24 0.004397105 0.008704831 0.001776866 0.003675728
7    ambient    Dominant RBAI2012 13 0.025836265 0.011880315 0.003295007 0.007179203
8    ambient    Dominant RBAI2013 13 0.025992636 0.015162901 0.004205432 0.009162850
9    ambient  Codominant RBAI2012 26 0.018067329 0.011830940 0.002320238 0.004778620
10   ambient  Codominant RBAI2013 26 0.015595275 0.012467140 0.002445007 0.005035587
11   ambient Subordinate RBAI2012 33 0.006073904 0.008287442 0.001442658 0.002938599
12   ambient Subordinate RBAI2013 35 0.003239033 0.006846507 0.001157271 0.002351857

我尝试了以下代码,产生了这个情节:

p <- ggplot(growth,aes(class,value,colour=treatment,group=variable))
pd<-position_dodge(.9)
# se= standard error; ci=confidence interval
p + geom_errorbar(aes(ymin=value-se,ymax=value+se),width=.1,position=pd,colour="black") + geom_point(position=pd,size=4) + geom_line(position=pd) + 
  theme_bw() + theme(legend.position=c(1,1),legend.justification=c(1,1))

enter image description here

线条应该在每个x轴类别中链接相同颜色的点,但显然它们不会。拜托,你能不能帮我正确划线(例如蓝色和红色,红色,红色,“主导”级,不同的线条为“共显”级。 另外,你知道如何在x标签中包含我正在分组的变量(即“RBAI2012”,“RBAI2013”​​? 非常感谢

4 个答案:

答案 0 :(得分:1)

为了区分“变量”的不同级别,您可以引入第四个aes stetic:shape。首先定义一个新的分组变量,即'treatment'和'variable'的组合,它有四个级别。将组,颜色和形状映射到此变量。然后使用scale_colour_manualscale_shape_manual设置两个级别的颜色,这对应于两个级别的“处理”。同样,定义两个“变量”形状。

growth$grp <- paste0(growth$treatment, growth$variable)

ggplot(data = growth, aes(x = class, y = value, group = grp,
                      colour = grp, shape = grp)) +
  geom_point(size = 4, position = pd) +
  geom_line(position = pd) +
  geom_errorbar(aes(ymin = value - se, ymax = value + se), colour = "black",
                position = pd, width = 0.1) +
  scale_colour_manual(name = "Treatment:Variable",
                      values = c("red", "red","blue", "blue")) +
  scale_shape_manual(name = "Treatment:Variable",
                     values = c(19, 17, 19, 17))
  theme_bw() +
  theme(legend.position = c(1,1), legend.justification = c(1,1))

enter image description here

答案 1 :(得分:0)

一种选择是使用像这样的分面图:

p <- ggplot(growth, aes(x = class, y = value, group = treatment, color = treatment))
p + geom_point(size = 4) + facet_grid(. ~ variable) + geom_errorbar(aes(ymin=value-se,ymax=value+se),width=.1,colour="black") + geom_line()

如果你想在一张图上,另一种选择是定义一个结合治疗和变量的新变量:

growth$treatment_variable <- paste(growth$treatment, growth$variable)
p <- ggplot(growth, aes(x = class, y = value, group = treatment_variable, colour = treatment_variable))
pd<-position_dodge(.2)
p + geom_point(size = 4, position=pd) + geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.1, position=pd, colour="black") + geom_line(position=pd)

答案 2 :(得分:0)

您有太多的分组变量(variabletreatment)并将它们包含在单个图中可能会有点令人困惑。您可能想要使用分面,如下所示:

p <- ggplot(growth,aes(class,value,colour=treatment,group=treatment))
pd<-position_dodge(.9)
p + 
  geom_errorbar(aes(ymin=value-se,ymax=value+se),width=.1,position=pd,colour="black") +
  geom_point(position=pd,size=4) + geom_line(position=pd) + 
  theme_bw() + theme(legend.position=c(1,1),legend.justification=c(1,1)) +
  facet_grid(variable~treatment)

enter image description here

答案 3 :(得分:0)

可以这样做,但你需要破解它,因为你基本上在geom_line()上绘制不同的分组(变量+处理),而不是geom_point()geom_errorbar()调用

您需要使用ggplot_build()取回呈现的数据,并根据现有点数据绘制geom_line(),按颜色分组:

p <- ggplot(growth)          # move the aes() into the individual charts
pd<-position_dodge(.9)       # leave dodge as is
se<-0.01                     # faked this

p <- p + 
  geom_point(aes(x=factor(class),y=value,colour=treatment,group=variable),position=pd,size=4) +
  theme_bw() + theme(legend.position=c(1,1),legend.justification=c(1,1)) +
  geom_errorbar(aes(x=factor(class),ymin=value-se,ymax=value+se,colour=treatment,group=variable),position=pd,width=.1,colour="black")

b<-ggplot_build(p)$data[[1]]  # get the ggpolt rendered data for this panel

p + geom_line(data=b,aes(x,y,group=colour), color=b$colour) # plot the lines

enter image description here