4

画像を 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,
)
4

2 に答える 2

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)

ここに画像の説明を入力

于 2013-02-27T20:54:00.667 に答える
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")
于 2013-07-05T14:50:14.350 に答える