R 添加图片

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)

R 添加图片_第1张图片

通过ggplot2

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

R 添加图片_第2张图片

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)

R 添加图片_第3张图片

你可能感兴趣的:(R,数据可视化)