鉴于现有的剧情对象是有可能添加层UNDERNEATH现有图层?
例如,在下面的图表中,是有可能添加geom_boxplot()到P使得出现箱线图下方 geom_point()?
## Starting from:
library(ggplot2)
P <- ggplot(data=dat, aes(x=id, y=val)) + geom_point()
## This adds boxplot, but obscures some of the points
P + geom_boxplot()
Run Code Online (Sandbox Code Playgroud)
# Which is essentially
ggplot(data=dat, aes(x=id, y=val)) + geom_boxplot() + geom_point()
## However, this involves re-coding all of P (after the point insertion of the new layer).
## which is what I am hoping to avoid.
Run Code Online (Sandbox Code Playgroud)

额外问题: 如果现有图中有多个图层,是否可以指示插入新图层的具体位置(相对于现有图层)?
set.seed(1)
N <- 100
id …Run Code Online (Sandbox Code Playgroud) 我正在使用ggplot()和geom_line()为随着时间的推移而发展的价值走廊创建线图。
有时可能会发生上限低于下限(我将其称为“反转”),并且我想突出显示在我的图中发生这种情况的区域,例如使用不同的背景颜色。
搜索 Google 和 StackOverflow 都没有给我带来任何结果。
这是一个人为的例子:
library(tidyverse)
library(RcppRoll)
set.seed(42)
N <- 100
l <- 5
a <- rgamma(n = N, shape = 2)
d <- tibble(x = 1:N, upper = roll_maxr(a, n = l), lower = roll_minr(a + lag(a), n = l)) %>% mutate(inversion = upper < lower)
dl <- pivot_longer(d, cols = c("upper", "lower"), names_to = "Bounds", values_to = "bound_vals")
ggplot(dl, mapping = aes(x = x, y = bound_vals, color = Bounds)) + …Run Code Online (Sandbox Code Playgroud)