How do I overlay an image on to a ggplot?

后端 未结 3 1080
礼貌的吻别
礼貌的吻别 2020-12-15 22:11

I\'d like to read an image from the web. e.g.

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

and insert it into the top right of a ggplot

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 22:36

    You are looking for annotation_raster and readPNG

    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()
    

    enter image description here

    These new features (and more examples) are described here

提交回复
热议问题