将图像插入ggplot2

djq*_*djq 34 r ggplot2

是否可以geom_line()ggplot2图表下方插入光栅图像或pdf图像?

我希望能够快速地在先前计算的绘图上绘制数据,这需要花费很长时间才能生成,因为它使用了大量数据.

我仔细阅读了这个例子.但是,由于它超过一年,我认为现在可能有不同的方式.

bap*_*ste 66

尝试?annotation_customggplot2

例,

library(png)
library(grid)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)

qplot(1:10, 1:10, geom="blank") +
  annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
  geom_point()
Run Code Online (Sandbox Code Playgroud)

  • 我添加了一个位图示例.使用矢量图像,您需要使用`grImport`包创建一个grob. (5认同)
  • @kennyB因为我不明白的原因,grobs需要有独特的名字.试试`g2 = editGrob(g,name ="newgrob")` (3认同)

D.H*_*ley 18

只需添加来自极好的Magick包的更新:

library(ggplot2)
library(magick)
library(here) # For making the script run without a wd
library(magrittr) # For piping the logo

# Make a simple plot and save it
ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point() + 
  ggtitle("Cars") +
  ggsave(filename = paste0(here("/"), last_plot()$labels$title, ".png"),
         width = 5, height = 4, dpi = 300)
Run Code Online (Sandbox Code Playgroud)

汽车

# Call back the plot
plot <- image_read(paste0(here("/"), "Cars.png"))
# And bring in a logo
logo_raw <- image_read("http://hexb.in/hexagons/ggplot2.png") 

# Scale down the logo and give it a border and annotation
# This is the cool part because you can do a lot to the image/logo before adding it
logo <- logo_raw %>%
  image_scale("100") %>% 
  image_background("grey", flatten = TRUE) %>%
  image_border("grey", "600x10") %>%
  image_annotate("Powered By R", color = "white", size = 30, 
                 location = "+10+50", gravity = "northeast")

# Stack them on top of each other
final_plot <- image_append(image_scale(c(plot, logo), "500"), stack = TRUE)
# And overwrite the plot without a logo
image_write(final_plot, paste0(here("/"), last_plot()$labels$title, ".png"))
Run Code Online (Sandbox Code Playgroud)

带徽标的汽车


Val*_*tin 16

也可以使用cowplotR包(cowplot是一个强大的扩展ggplot2).它还需要magick包装.检查牛皮插图的介绍.

以下是PNG和PDF背景图像的示例.

library(cowplot)
library(magick)

my_plot <- 
  ggplot(data    = iris, 
         mapping = aes(x    = Sepal.Length, 
                       fill = Species)) + 
  geom_density(alpha = 0.7)

# Example with PNG (for fun, the OP's avatar - I love the raccoon)
ggdraw() +
  draw_image("https://i.stack.imgur.com/WDOo4.jpg?s=328&g=1") +
  draw_plot(my_plot)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

# Example with PDF
ggdraw() +
  draw_image(file.path(R.home(), "doc", "html", "Rlogo.pdf")) +
  draw_plot(my_plot)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


str*_*agu 5

跟进@baptiste的回答,如果您使用更具体的注释功能,则不需要加载grob包并转换图像annotation_raster()

更快的选项可能如下所示:

# read picture
library(png)
img <- readPNG(system.file("img", "Rlogo.png", package = "png"))
# plot with picture as layer
library(ggplot2)
ggplot(mapping = aes(1:10, 1:10)) +
  annotation_raster(img, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) +
  geom_point()
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2021 年 2 月 16 日创建(v1.0.0)