如何在r中设置插入图像的颜色 [英] how to set color of inserted image in r

查看:96
本文介绍了如何在r中设置插入图像的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将图像插入到R图中,并在我这样做时设置它的颜色?我想插入给定数据集的轮廓,并将其设置为与我选择用于绘制相应数据点的颜色相匹配。我对图形如何管理有一个很好的理解 - 在一般的计算机系统和R中 - 这可能会告知这个问题的答案。

Is there a way to insert an image into a plot into R and set its color when I do so? I'd like to insert a silhouette for a given data set and set it to match the color I've chosen for plotting the corresponding the data points. I don't have a strong understanding of how graphics are managed - in computer systems in general, and in R - which may inform the answer to this question.

代码下面会插入图像,但我无法找出一种方法来改变颜色。

The code below will insert the image, but I can't figure out a way to change the color.

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


推荐答案

我不明白这里的轮廓究竟是什么意思。但对我来说,光栅是一种颜色矩阵。所以你可以改变它的颜色。这里是一个示范。我使用 grid 包中的 grid.raster 。但这与 rasterImage

i don't understand exactly what do you mean by silhouette here. But for me a raster is a matrix of color. So you can change its color. here a demonstration. I am using, grid.raster from the grid package. But it the same thing with rasterImage

这里是一样的例子:

here an example:

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)

这篇关于如何在r中设置插入图像的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆