gpt4 book ai didi

r - 用图像注释 ggplot2 方面

转载 作者:行者123 更新时间:2023-12-02 05:25:29 26 4
gpt4 key购买 nike

假设我正在为 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 不足以应对挑战!

最佳答案

不是很优雅,但是你可以在条形标签的顶部添加 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)

关于r - 用图像注释 ggplot2 方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13152067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com