R 如何在保存图像质量的同时保存添加了徽标的ggplot

R 如何在保存图像质量的同时保存添加了徽标的ggplot,r,ggplot2,R,Ggplot2,我希望在添加徽标后,使用ggsave功能保存下面的ggplot2图表 # Load packages and dataset library(ggplot2) library(magick) data(mpg, package="ggplot2") # Load a logo from Github logo <- image_read("https://jeroen.github.io/images/frink.png") # Plot C

我希望在添加徽标后,使用
ggsave
功能保存下面的
ggplot2
图表

# Load packages and dataset
library(ggplot2)
library(magick)

data(mpg, package="ggplot2")

# Load a logo from Github

logo <- image_read("https://jeroen.github.io/images/frink.png")

# Plot Code

ggplot(mpg, aes(cty, hwy)) +
  geom_count(col="tomato3", show.legend=F)

# Add logo to plot

grid::grid.raster(logo, x = 0.04, y = 0.03, just = c('left', 'bottom'), width = unit(1, 'inches'))


这是一种非常笨拙的方法,在
gtable
级别插入光栅,然后将gtable作为自定义注释重新插入。很抱歉,我找不到更简洁的方法

库(ggplot2)
图书馆(magick)
#>链接到ImageMagick 6.9.11.57
#>启用的功能:cairo、freetype、fftw、ghostscript、heic、lcms、pango、raw、rsvg、webp
#>禁用的功能:fontconfig,x11
数据(mpg,package=“ggplot2”)
#从Github加载徽标

logo或使用老式方式,首先创建一个设备

png()
ggplot(mpg, aes(cty, hwy)) +
  geom_count(col="tomato3", show.legend=F)
grid::grid.raster(logo, x = 0.04, y = 0.03, just = c('left', 'bottom'), width = unit(1, 'inches'))
dev.off()

您可以在?ggsave?中找到一个小段落,在不使用ggsave()的情况下保存图像部分。

看起来您需要这样做?是的,这可能是更好的方法!我知道你已经接受了我的答案,但我真的认为@tjebo的答案是解决这个问题的更好方法。如果你接受他的回答,我不会有什么不好的感觉。
png()
ggplot(mpg, aes(cty, hwy)) +
  geom_count(col="tomato3", show.legend=F)
grid::grid.raster(logo, x = 0.04, y = 0.03, just = c('left', 'bottom'), width = unit(1, 'inches'))
dev.off()