gpt4 book ai didi

r - lapply'd geoms 失去了因子排序

转载 作者:行者123 更新时间:2023-12-04 11:34:47 26 4
gpt4 key购买 nike

ggplot2 – issue with overlay of lines and errorbars ,我提出了一个使用 lapply(.) 的答案以特定顺序生成几何图形组,以便每个级别的点/线/误差条将分层在一起。然而,似乎factor在此过程中会丢失级别的顺序。
第一个代码块生成原始图:图例中元素的顺序是正确的(但几何分层不是,这在我的回答之前)。

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)
ggplot2 with correct order of 'treatment'
我建议的答案使用 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)
)
})
ggplot2, order of levels is lost
请注意,图例的级别顺序不正确。
levels(d2$treatment)
# [1] "HIGH" "MEDIUM" "LOW"
“如何更改图例元素顺序”的答案几乎总是“使用 factor(., levels=.)”,但已完成并被忽略。
为什么顺序会丢失,更重要的是我们如何保留图例中的顺序(当图层作为几何列表添加时)?
(PS:如果你对如何更好地控制分层有意见,请在 the previous question 中讨论。这个问题是基于出于某种原因使用 lapply 的要求来生成geom,因此如何防止该进程掉线因子水平。谢谢!)

最佳答案

不确定细节。所以我会说这只是部分答案,因为它缺乏详细的解释。但至少它提供了一个可能的解决方案。我的猜测是它与参数有关 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)
)
})

关于r - lapply'd geoms 失去了因子排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68863522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com