【发布时间】:2013-11-27 17:54:21
【问题描述】:
给定一个现有的绘图对象,是否可以在现有图层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()
预期输出:
# 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.
额外问题:如果现有绘图中有多个图层,是否可以指出具体插入新图层的位置(相对于现有图层)?
样本数据
set.seed(1)
N <- 100
id <- c("A", "B")
dat <- data.frame(id=sample(id, N, TRUE), val=rnorm(N))
【问题讨论】:
-
我认为这是不可能的,除非你保存最低的公共层并从那里重新混合。
-
@Maiasaura,谢谢。这就是我现在正在做的事情,但原来的
P相当复杂,我希望避免为我需要进行的每个小修改重新编码。 -
您可以轻松更改最终绘图中的图层顺序,
p2$layers = rev(p2$layers) -
@baptiste.. 我不想修改所有图层,但感谢您指出正确的方向!
-
你明白了,这只是一个列表