使用指定大小的图像注释 ggplot2 facet 背景的特定区域

Annotate specific area of ggplot2 facet backgrounds with images of a specified size

我的问题与this one类似;我想在左下角用不同的图像注释每个方面。

使用 gtable_add_grob 我可以用这样的图像替换刻面:

library(ggplot2)
library(gtable)
library(RCurl)
library(png)

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))
shark <- readPNG(getURLContent("http://i.imgur.com/EOc2V.png"))
tiger <- readPNG(getURLContent("http://i.imgur.com/zjIh5.png"))

facets <- grep("panel", g$layout$name)
new_grobs <- list(rasterGrob(shark, width=1, height=1),
                  rasterGrob(tiger, width=1, height=1))
g2 <- with(g$layout[facets,],
          gtable_add_grob(g, new_grobs,
                          t=t, l=l, b=b, r=r, name="pic_predator") )        
grid.draw(g2)

然而,我真正想要的是这样的东西,但我想不出合适的 gtable 命令来缩小图像并将其放置在每个面上:

如果有必要,我很高兴解决方案不使用 gtable

控制widthheight参数在rasterGrob缩小图像,并设置xy位置(或使用hjustvjust 我想)控制图像的位置。

library(ggplot2)
library(gtable)
library(RCurl)
library(png)

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))
shark <- readPNG(getURLContent("http://i.imgur.com/EOc2V.png"))
tiger <- readPNG(getURLContent("http://i.imgur.com/zjIh5.png"))

facets <- grep("panel", g$layout$name)
new_grobs <- list(rasterGrob(shark, width=.2, height=.05, x = .2, y = .05),
                  rasterGrob(tiger, width=.2, height=.05, x = .2, y = .05))
g2 <- with(g$layout[facets,],
          gtable_add_grob(g, new_grobs,
                          t=t, l=l, b=b, r=r, name="pic_predator") )        
grid.draw(g2)