在R中添加图片以绘图 [英] Adding a picture to plot in R
问题描述
我正在尝试将图片(jpeg,png不在乎)添加到由布局功能定义的绘图中.例如:
I'm trying to add a picture (jpeg,png doesn't care) to a plot which is defined by the layout function. For example:
a<-c(1,2,3,4,5)
b<-c(2,4,8,16,32)
m <- matrix(c(1,1,1,1,2,3,2,3), nrow = 2, ncol = 4)
layout(m); hist(a);boxplot(a~b);plot(b~a)*
我要添加图像而不是位置1的直方图(在我的情况下,它是地图)
Instead of the histogram on position 1 I want to add an image (In my case it's a map)
我不知道如何处理jpeg包,也许您可以帮我!
I don't know how to deal with the jpeg package, maybe you can help me!
推荐答案
您需要通过png
和jpeg
包读取png
或jpeg
文件.然后,使用rasterImage
功能可以在绘图上绘制图像.假设您的文件是myfile.jpeg
,则可以尝试以下操作:
You need to read your png
or jpeg
file through the png
and jpeg
packages. Then, with the rasterImage
function you can draw the image on a plot. Say that your file is myfile.jpeg
, you can try this:
require(jpeg)
img<-readJPEG("myfile.jpeg")
#now open a plot window with coordinates
plot(1:10,ty="n")
#specify the position of the image through bottom-left and top-right coords
rasterImage(img,2,2,4,4)
上面的代码将在(2,2)和(4,4)点之间绘制图像.
The above code will draw the image between the (2,2) and (4,4) points.
这篇关于在R中添加图片以绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!