R 添加图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/arcers/article/details/84304136

R 添加图片

在R的绘图对象中添加图片,以下列举两种方法。

图片对象

library(ggplot2)
gg <- ggplot(mpg, aes(displ, hwy, colour = class)) +  geom_point()

静态图片

library(png)
logo <- readPNG('E:/logo.png')

通过 grid

grid.newpage()
print(gg)
vp <- viewport(x = 0.6, y = 0.3, width = 0.2, height = 0.2)
grid.raster(logo, vp = vp)

在这里插入图片描述

通过ggplot2

gg + annotation_custom(
    rasterGrob(logo, width=unit(1,"npc"), height=unit(1,"npc")),
     xmin = 5, xmax = 6, ymin = 30, ymax = 40
    )

在这里插入图片描述

plot 绘制图片

  par(mar = c(0,0,0,0))
  plot(0, 1, type = 'n', xlim = c(0,1), ylim = c(0,1), ann = F, bty = "n", xaxt = "n", yaxt ="n")
  rasterImage(logo, 0.25,0.25,0.75,0.75)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/arcers/article/details/84304136
R
R: