假设我正在绘制4个人的数据: Alice,Bob,Chuck和Dana。我正在使用ggplot2制作一个分面图,每个人只有一个分面。我在磁盘上还有4个镜像: Alice.png、Bob.png、Chuck.png和Dana.png。(显然,这是一个需要扩展到4个以上方面的合成示例:)
有没有一种方法可以用相应的图像来注释每个方面,理想情况下可以不使用方面标签(尽管我对标签下面的图像很满意)?也许是类似于这里使用的技术:Use image instead of labels in ggplot2 legend?我试着通读了各种注解方法的文档,但我的R-fu不足以应对挑战!
发布于 2012-10-31 14:31:44
不是很优雅,但你可以在标签上添加grobs,
library(ggplot2)
d <- expand.grid(x=1:2,y=1:2, f=letters[1:2])
p <- qplot(x,y,data=d) + facet_wrap(~f)
g <- ggplot_gtable(ggplot_build(p))
library(gtable)
library(RCurl)
library(png)
shark <- readPNG(getURLContent("http://i.imgur.com/EOc2V.png"))
tiger <- readPNG(getURLContent("http://i.imgur.com/zjIh5.png"))
strips <- grep("strip", g$layout$name)
new_grobs <- list(rasterGrob(shark, width=1, height=1),
rasterGrob(tiger, width=1, height=1))
g <- with(g$layout[strips,],
gtable_add_grob(g, new_grobs,
t=t, l=l, b=b, r=r, name="strip_predator") )
grid.draw(g)编辑:您也可以直接替换grobs,
strips <- grep("strip", names(g$grobs))
new_grobs <- list(rectGrob(gp=gpar(fill="red", alpha=0.2)),
rectGrob(gp=gpar(fill="blue", alpha=0.2)))
g$grobs[strips] <- new_grobs
grid.draw(g)https://stackoverflow.com/questions/13152067
复制相似问题