参考以下链接(使用ggplot2对齐两个图表),我能够在一个共同的x轴下绘制两个“y”变量。现在我想做的是只能向其中一个facet添加一个geom_point层。这个层使用不同的数据集(d3),但结构与d1相同。当我添加这个层时,它会被用于两个facets。是否可能只将点层叠放在上面的facet中。
library(ggplot2)
x <- seq(1992, 2002, by = 2)
d1 <- data.frame(x = x, y = rnorm(length(x)))
xy <- expand.grid(x = x, y = x)
d2 <- data.frame(x = xy$x, y = xy$y, z = jitter(xy$x + xy$y))
d3 <- data.frame(x = x, y = 3+rnorm(length(x)))
d1$panel <- "a"
d2$panel <- "b"
d1$z <- d1$x
d <- rbind(d1, d2)
p <- ggplot(data = d, mapping = aes(x = x, y = y))
p <- p + facet_grid(panel ~ ., scale = "free")
p <- p + layer(data = d1, geom = c( "line"), stat = "identity")
###*p <- p + layer(data = d3, geom = c( "point"))* - This is the layer I intend to add only to the top panel
p <- p + layer(data = d2, geom = "line", stat = "identity")
p