我使用 ggplot2 中的 facet_wrap
创建了一个多面板图,并希望将不同的图像添加到每个面板中。
annotation_custom
可以用于在 ggplot2 中插入图像,但对于所有面板都是相同的。
以下是添加 R logo 的示例:
library(ggplot2)
# Create dataset
df <- data.frame(
x = rep(seq(1, 5), times = 2),
y = rep(seq(1, 5), times = 2),
z = rep(seq(1, 2), each = 5)
)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)
ggplot(df) +
geom_point(aes(x, y)) +
annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
facet_wrap(~z)
img2
是由 img
生成的。
img2 <- img * 0.5
在ggplot2中是否可以插入不同的图像(例如将img
插入到第1个面板,将img2
插入到第2个面板)?
感谢任何建议。如果我的问题不够清晰,请告诉我。
?annotation_custom
: 这是一种特殊的“geom”,旨在用作在每个面板中都相同的静态注释。该链接可能有所帮助:https://dev59.com/E4_ea4cB1Zd3GeqPPYfn?answertab=votes#tab-top。 - user20650annotation_custom2
似乎可以给出您想要的控制:ggplot(df) + geom_point(aes(x, y)) + facet_wrap(~z) + annotation_custom2(g, data=data.frame(z=1))
- user20650