【问题标题】:Position dodge on a flipped axis with error bars in ggplot2在 ggplot2 中使用误差线在翻转轴上定位闪避
【发布时间】:2017-09-02 21:45:21
【问题描述】:

我正在使用以下数据框在 ggplot2 中创建深度图:

TTTTOM<- data.frame(Treatment=c("Mineral", "Mineral", "Mineral", "OM", "OM", "OM", "Veg", "Veg", "Veg"), Depth2=c(-7.5, -4.5, -1.5, -7.5, -4.5, -1.5, -7.5, -4.5, -1.5), mean=c(2.83, 3.33, 3.16, 9.16, 11.17, 11.67, 4.83, 5, 5.17), se=c(0.7, 1.12, 0.65, 2.41, 3.28, 3.12, 1.83, 1.71, 0.95))

我已将此代码与 ggplot2 一起使用来创建绘图,但位置闪避似乎与误差条的工作方式与对线和点的工作方式不同。我需要我的误差线来贯穿我的观点(即让它们以相同的数量躲避)。谢谢你的帮助。

pd <- position_dodge(0.3) 
ggplot(TTTTOM, aes(x=Depth2, y=mean, linetype=Treatment)) + 
  geom_line(aes(linetype=Treatment), colour="black", size=0.75, position=pd) +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), linetype=1, colour="black", width=.1, position=pd) +
  geom_point(colour="black",shape=16, size=2.0, position=pd) +
  scale_linetype_manual(values=c(1, 2, 4), labels=c("Organic", "Mineral", "Vegetated"))+
  geom_vline(aes(xintercept=0), linetype="dashed")+
  theme(legend.position="none",
    axis.title.x=element_text(size=18, margin=margin(10,0,0,0)),
    legend.title=element_blank(),
    legend.text=element_text(size=18),
    legend.key.width=unit(2,"line"),
    axis.text=element_text(size=18),
    axis.title.y=element_text(size=16, margin=margin(0,10,0,0)),
    plot.margin = unit(c(.25, .25, .25, .25), "in"))+
  labs(x=paste("Distance from \n sediment surface (cm)"), y="% Total OM") +
  expand_limits(x=c(-10,0))+
  coord_flip()

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您对 geom_errorbar() 的调用需要一个组参数;在这种情况下,group=Treatment

    pd <- position_dodge(.3) 
    
    ggplot(TTTTOM, aes(x=Depth2, y=mean, linetype=Treatment)) + 
      geom_line(aes(linetype=Treatment), colour="black", size=0.75, position=pd) +
      geom_errorbar(aes(ymin=mean-se, ymax=mean+se, group=Treatment), 
                linetype=1, colour="black", width=.1, position=pd) +
      geom_point(colour="black",shape=16, size=2.0, position=pd) +
      scale_linetype_manual(values=c(1, 2, 4), labels=c("Organic", "Mineral", "Vegetated"))+
      geom_vline(aes(xintercept=0), linetype="dashed")+
      theme(legend.position="none",
            axis.title.x=element_text(size=18, margin=margin(10,0,0,0)),
            legend.title=element_blank(),
            legend.text=element_text(size=18),
            legend.key.width=unit(2,"line"),
            axis.text=element_text(size=18),
            axis.title.y=element_text(size=16, margin=margin(0,10,0,0)),
            plot.margin = unit(c(.25, .25, .25, .25), "in"))+
      labs(x=paste("Distance from \n sediment surface (cm)"), y="% Total OM") +
      expand_limits(x=c(-10,0))+
      coord_flip()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多