在ggplot2 – 线条和误差条的叠加问题中,我建议了一个用于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)
Run Code Online (Sandbox Code Playgroud)
我建议的答案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)
)
})
Run Code Online (Sandbox Code Playgroud)
请注意,图例的级别顺序不正确。
levels(d2$treatment)
# [1] "HIGH" "MEDIUM" "LOW"
Run Code Online (Sandbox Code Playgroud)
“如何更改图例元素顺序”的答案几乎总是“使用factor(., levels=.)”,但已经完成并被忽略。
为什么顺序会丢失,更重要的是我们如何保留图例中的顺序(当图层作为几何列表添加时)?
(PS:如果您对如何更好地控制分层有意见,请在上一个问题中讨论。这个问题基于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)
)
})
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |