有没有办法将图像插入到R中的图形中并在我这样做时设置其颜色?我想为给定的数据集插入一个轮廓,并将其设置为与我为绘制相应的数据点所选择的颜色相匹配。我对图像的管理方式没有深刻的理解 - 在计算机系统中,在R中 - 可能会为这个问题提供答案。
下面的代码将插入图像,但我无法找到改变颜色的方法。
require(jpeg)
thiscolor <- "red"
plot(x=c(1, 4), y=c(1, 2), main="test plot", col=thiscolor)
thispic <- readJPEG(<insert path for any image here>)
rasterImage(thispic,
xleft=3, xright=3.5,
ytop=2, ybottom=1.8,
)
答案 0 :(得分:3)
我不明白你的剪影究竟是什么意思。但对我来说,光栅是一种颜色矩阵。所以你可以改变它的颜色。这里有一个示范。我正在使用grid.raster
包中的grid
。但rasterImage
这里有一个例子:
library(png)
library(grid)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
## convert it to a raster, interpolate =F to select only sample of pixels of img
img.r <- as.raster(img,interpolate=F)
## Change all the white to a blanck
img.r[img.r == "#00000000"] <- 'red'
plot.new()
grid.raster(img.r)
答案 1 :(得分:1)
非常感谢你!
因为我使用的是R颜色名称,所以我不得不做一些(不合适的)颜色转换。但是你的代码正是我所需要的!谢谢!
#convert image to raster
thispic.r <- as.raster(thispic)
#get the hex color
rgbratios <- col2rgb(thiscolor)/255
thiscolorhex <- rgb(red=rgbratios[1],
green=rgbratios[2],
blue=rgbratios[3])
#convert the non-white part of the image to the hex color
thispic.r[thispic.r!="#FFFFFF"] <- thiscolorhex
#plot it
grid.raster(thispic.r, x=xval, y=yval, just="center")