如何将图像叠加到ggplot上?

Mai*_*ura 15 png r ggplot2

我想从网上阅读一张图片.例如

http://api.altmetric.com/donut/502878_64x64.png

并将其插入a的右上角 ggplot

df <- data.frame(x=1:10, y=sample(1:100,10))
# a fake plot to try it on.
ggplot(df, aes(x,y)) + geom_point(size = 2)
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

mne*_*nel 20

你正在寻找annotation_rasterreadPNG

mypngfile <- download.file('http://api.altmetric.com/donut/502878_64x64.png', destfile = 'mypng.png', mode = 'wb')
library(png)
mypng <- readPNG('mypng.png')


p <- qplot(mpg, wt, data = mtcars) + theme_bw()
p + annotation_raster(mypng, ymin = 4.5,ymax= 5,xmin = 30,xmax = 35) + 
    geom_point()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这里描述这些新功能(以及更多示例)


Mai*_*ura 5

正确的解决方案是:

# This was one of my issues, reading a png from the web
my_image <-  readPNG(getURLContent('http://path.to/image.png'))
p1 + annotation_raster(my_image, ymin = 4,ymax= 5,xmin = 30,xmax = 40)
Run Code Online (Sandbox Code Playgroud)