【问题标题】:Sorting errorbars with a faceted grid?用多面网格对误差线进行排序?
【发布时间】:2016-03-03 04:29:13
【问题描述】:

我已经为此苦苦挣扎了几天,但找不到一段代码可以提供帮助。我有一个在 ggplot2 中刻面的多变量条形图,我终于解决了它,所以它看起来很棒并且传达了我想要显示的数据。不幸的是,当我将误差线添加到图形时,它们都是无序的并且在整个条形图中浮动。 (barplot example of the bane of my existence)

数据:(谢谢,我忘了 dput)

 > dput(plant_ag)

    structure(list(worms = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
     2L, 1L, 2L, 1L, 2L), .Label = c("No", "Yes"), class = "factor"), 
    poll_level = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 
    2L, 2L, 3L, 3L), .Label = c("Low", "Medium", "Very"), class = "factor"), 
    variable = structure(c(7L, 7L, 7L, 7L, 7L, 7L, 10L, 10L, 
    10L, 10L, 10L, 10L), .Label = c("biomass", "cu_seed", "pb_seed", 
    "zn_seed", "cu_buck", "pb_buck", "zn_buck", "cu_rye", "pb_rye", 
    "zn_rye"), class = "factor"), x.mean = c(0.9945992584, 4.645085363, 
    198.390869802, 58.43054462, 3795.1789451, 2883.8860731, 942.59827934, 
    1313.1415081, 707.165653286, 1529.33515045, 3811.442312, 
    6149.4490085), x.sd = c(3.14519901565836, 14.68904967299, 
    176.524518210961, 61.362498383762, 554.023490328341, 541.012498962109, 
    224.078432824798, 221.039876468939, 468.567696912978, 1021.25312103485, 
    2431.58456925777, 3004.85636935088), x.n = c(10, 10, 10, 
    10, 10, 10, 10, 10, 10, 10, 10, 10), se = c(0.9945992584, 
    4.645085363, 55.8219540410509, 19.4045257811089, 175.197610667382, 
    171.083173933975, 70.8598222247401, 69.8989463364104, 148.174116022446, 
    322.948593002573, 768.934556217399, 950.219016881294), 
    names = c("No EW/ Low Soil", 
    "Yes EW/ Low Soil", "No EW/ Medium Soil", "Yes EW/ Medium Soil", 
    "No EW/ Very Soil", "Yes EW/ Very Soil", "No EW/ Low Soil", 
    "Yes EW/ Low Soil", "No EW/ Medium Soil", "Yes EW/ Medium Soil", 
    "No EW/ Very Soil", "Yes EW/ Very Soil"), 
     ymin = c(0, 8.88178419700125e16, 
    142.568915760949, 39.0260188388911, 3619.98133443262, 2712.80289916603, 
    871.73845711526, 1243.24256176359, 558.991537263554, 1206.38655744743, 
    3042.5077557826, 5199.22999161871), ymax = c(1.9891985168, 
    9.290170726, 254.212823843051, 77.8350704011089, 3970.37655576738, 
    3054.96924703397, 1013.45810156474, 1383.04045443641, 855.339769308446, 
    1852.28374345257, 4580.3768682174, 7099.66802538129)), 
 .Names =   c("worms", 
"poll_level", "variable", "x.mean", "x.sd", "x.n", "se", "names", 
"ymin", "ymax"), row.names = c(NA, -12L), class = "data.frame")
> 

代码:

########### 图表从这里开始
library(ggplot2)

p <- ggplot(data = plant_ag, aes(x = factor(worms), fill = factor(variable), y = x.mean,
                                 group=variable))
p <- p + geom_bar(colour="black",stat = "identity",position = position_dodge(.95))
p <- p + facet_grid(. ~ poll_level)
p <- p + geom_errorbar(aes(ymin=plant_ag$ymin, ymax=plant_ag$ymax), width=0.2,  stat="identity", 
                       position = position_dodge(1))
          +  facet_wrap(worms ~poll_level, ncol=4) 
p <- p + theme_bw () + theme(legend.position = c(0.15, 0.8)) + 
  ggtitle("Zinc in Plants by Soil Pollution Level") +ylab("Zn concentration in plants, ppm") + 
  xlab("Earthworm Community Present")
p <- p + scale_fill_manual(values=c('darkgray','lightblue'), 
                           name="Experimental\nPlant",
                           labels=c("Buckwheat", "Rye")) 
p
######### 命令结束

非常非常欢迎任何帮助(或同情)!感谢你们所做的一切,这对我们所有的 N00B 来说都是一个难以置信的帮助。

【问题讨论】:

  • 我无法测试您的代码,因为您的数据不可重现(您需要粘贴到 dput(plant_ag) 的输出中,以便将数据复制并粘贴到 R 会话中) .但是,目前,从aes(ymin=plant_ag$ymin, ymax=plant_ag$ymax) 更改为aes(ymin=ymin, ymax=ymax) 是否可以解决您的问题?
  • 另外,你不需要所有的p &lt;- p + ...。摆脱除第一个 p 之外的所有语句,然后将每个语句与 + 链接在一起,您可以一次创建整个情节。例如,ggplot(mydata, aes(x,y)) + geom_bar() + scale_fill_manual() + etc.。为了便于阅读,请在每个 + 之后添加一个换行符。
  • 我试图摆脱 p

标签: r ggplot2 facet errorbar


【解决方案1】:

geom_errorbar 中更改为aes(ymin=ymin, ymax=ymax) 解决了错误栏的排序问题。在对ggplot 函数的初始调用中,您已将数据框传递给ggplot,无需再次引用它。在geom_errorbar 中使用aes 中的数据框名称会覆盖ggplot 设置的沿x 轴的顺序,因此误差线是根据它们在数据框中的顺序绘制的,而不是根据它们在数据框中的顺序来绘制阴谋。

另外,您的代码同时具有facet_wrapfacet_grid,但您只能使用其中一个。下面是更新的情节代码,facet_wrap 被注释掉了。我还设置了 y 比例,因为您的数据对于弄乱绘图的误差条之一具有巨大的价值。

下面的代码还展示了如何使用+ 将绘图组件链接在一起。

p <- ggplot(data = plant_ag, aes(x = factor(worms), fill = factor(variable), 
                                 y = x.mean, group=variable)) + 
  geom_bar(colour="black", stat="identity", position=position_dodge(.95)) + 
  facet_grid(. ~ poll_level) + 
  geom_errorbar(aes(ymin=ymin, ymax=ymax), width=0.2, stat="identity",
                position = position_dodge(1)) +  
  scale_y_continuous(limits=c(0,8000)) +
#  facet_wrap(worms ~ poll_level, ncol=4) + 
  theme_bw() + theme(legend.position = c(0.15, 0.8)) + 
  ggtitle("Zinc in Plants by Soil Pollution Level") +
  labs(y="Zn concentration in plants, ppm",
       x="Earthworm Community Present") + 
  scale_fill_manual(values=c('darkgray','lightblue'), 
                    name="Experimental\nPlant",
                    labels=c("Buckwheat", "Rye")) 

【讨论】:

  • 天哪,太漂亮了!非常感谢。
  • 我看到你现在做了什么,因为我有机会用一把细齿梳子梳理它。谢谢你,我的论文也谢谢你!
  • 很高兴为您服务。祝你的论文好运!
猜你喜欢
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 2011-09-22
  • 2014-12-21
  • 1970-01-01
相关资源
最近更新 更多