如何在ggplot背景(而不是面板)上添加图像? [英] How to add an image on ggplot background (not the panel)?

查看:171
本文介绍了如何在ggplot背景(而不是面板)上添加图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用图像作为ggplot2图的背景?

How do I use an image as the background for a ggplot2 plot?

例如,代码:

mtcars %>% 
  ggplot(aes(cyl)) + 
  geom_bar() + 
  theme(plot.background = element_rect(fill = "black"))

结果如下:
并且背景颜色是黑色.

It results in the following:
and the background color is black.

如何选择图片作为背景,而不仅要选择其颜色?

How can I choose an image to be the background, and not only choose its color?

推荐答案

您可以将一个绘制在另一个之上,

you can plot one on top of the other,

library(ggplot2)
p <- ggplot(mtcars, aes(cyl)) + 
  geom_bar() +
  theme(plot.background = element_rect(fill=NA))

library(grid)
grid.draw(gList(rasterGrob(img, width = unit(1,"npc"), height = unit(1,"npc")), 
                ggplotGrob(p)))

这篇关于如何在ggplot背景(而不是面板)上添加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆