如何将图像叠加到ggplot上? [英] How do I overlay an image on to a ggplot?
本文介绍了如何将图像叠加到ggplot上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从网上读取图片。例如
http://api.altmetric.com/donut/502878_64x64.png
并将其插入 ggplot
的右上方, df < - data.frame(x = 1:10,y = sample(1:100,10))
#一个假的情节来试试它。
ggplot(df,aes(x,y))+ geom_point(size = 2)
我该怎么做?
和 readPNG
mypngfile < - download.file('http: (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()
这些新功能(以及更多示例) a href =http://cloud.github.com/downloads/hadley/ggplot2/guide-col.pdf =noreferrer> here
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
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)
How would I do this?
解决方案
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()
These new features (and more examples) are described here
这篇关于如何将图像叠加到ggplot上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文