【问题标题】:lapply'd geoms lose factor-orderinglapply'd 几何失去因子排序
【发布时间】:2021-08-20 23:32:06
【问题描述】:

ggplot2 – issue with overlay of lines and errorbars 中,我提出了一个使用lapply(.) 以特定顺序生成几何组的答案,以便每个级别的点/线/误差条 将分层在一起。但是,似乎factor 级别的顺序在此过程中丢失了。

这第一个代码块生成原始图:图例中元素的顺序是正确的(但geom分层不正确,这是我之前的答案)。

d2 <- structure(list(time_serie = c(1.3, 2.3, 3.3, 1.2, 2.2, 3.2, 1.1, 2.1, 3.1), treatment = structure(c(3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("HIGH", "MEDIUM", "LOW"), class = "factor"), mean_value = c(2.93173234758433, 5.60600521659944, 7.85452806402463, 5.25617444992531, 3.6695183776319, 4.57195128872991, 3.24979097663891, 4.59766399173532, 4.39298335579224), SE_value = c(0.232090045905285, 0.585377662916667, 0.679289569404838, 1.3130008364543, 0.849157470954342, 1.22194305280708, 1.21458843275054, 1.0620028602709, 0.949469468240659 )), row.names = c(NA, -9L), class = "data.frame")

library(ggplot2)
ggplot(aes(x = time_serie, y = mean_value, color = treatment, group = treatment), data = d2) +
  geom_errorbar(aes(ymin = mean_value - SE_value, ymax = mean_value + SE_value),
                width = 0.2, size = 2) +
  geom_point(aes(), size = 3) + 
  geom_line(aes(color = treatment), size = 2)

我建议的答案使用lapply 来控制图层的顺序。但是,这样做会丢失treatment 级别的顺序:

ggplot(aes(x = time_serie, y = mean_value, color = treatment, group = treatment), data = d2) +
  lapply(rev(levels(d2$treatment)), function(trtmnt) {
    list(
      geom_errorbar(data = ~ subset(., treatment == trtmnt),
                    aes(ymin = mean_value - SE_value, ymax = mean_value + SE_value),
                    width = 0.2, size = 2),
        geom_point(data = ~ subset(., treatment == trtmnt), aes(), size = 3),
        geom_line(data = ~ subset(., treatment == trtmnt), size = 2)
    )
  })

请注意,图例的级别顺序不正确。

levels(d2$treatment)
# [1] "HIGH"   "MEDIUM" "LOW"   

“如何更改图例元素顺序”的答案几乎总是“使用factor(., levels=.),但已经完成了,它被忽略了。

为什么顺序会丢失,更重要的是我们如何在图例中保留顺序(当图层作为几何列表添加时)?

(PS:如果你有关于如何更好地控制分层的cmets,请在the previous question讨论。这个问题是基于出于某种原因使用lapply生成geoms的要求,因此如何以防止该过程降低因子水平。谢谢!)

【问题讨论】:

  • 嗯。不确定细节。我的猜测是它与参数drop=TRUE 是离散比例的默认值有关。因此,在图例中保留顺序的一种选择是ggplot(...) + scale_color_discrete(drop = FALSE) + lapply(...)
  • @stefan,就是这样,请写下来! :-)
  • 完成。即使我只称它为部分答案。 (:
  • 我听到你在说什么,但scale_*_discrete 中的drop= 清楚地(在我事后看来)说“应该从量表中省略未使用的因子水平吗?”。这在某种程度上是有道理的,因为对于lapply 中的每个几何图形,在任何一层中看到的级别都是通过设计限制/降低的。也许这是一个小错误,这是在任何一层确定的,而不是汇总...

标签: r ggplot2


【解决方案1】:

不确定细节。所以我会说这只是部分答案,因为它缺乏详细的解释。但至少它提供了一个可能的解决方案。我的猜测是它与参数drop=TRUE 是离散比例的默认值有关。因此,保持图例中的顺序的一种选择是ggplot(...) + scale_color_discrete(drop = FALSE) + lapply(...)

library(ggplot2)

ggplot(aes(x = time_serie, y = mean_value, color = treatment, group = treatment), data = d2) +
  scale_color_discrete(drop = FALSE) +
  lapply(rev(levels(d2$treatment)), function(trtmnt) {
    list(
      geom_errorbar(data = ~ subset(., treatment == trtmnt),
                    aes(ymin = mean_value - SE_value, ymax = mean_value + SE_value),
                    width = 0.2, size = 2),
      geom_point(data = ~ subset(., treatment == trtmnt), aes(), size = 3),
      geom_line(data = ~ subset(., treatment == trtmnt), size = 2)
    )
  })

【讨论】:

    猜你喜欢
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 2018-05-14
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多