[英]How can I plot a image with (x,y,r,g,b) coordinates using ggplot2?
我有一個數據框image.rgb
,我已經為其中的每個坐標加載了r,g,b值(使用jpeg
和reshape
包)。 它現在看起來像:
> head(image.rgb)
y x r g b
1 -1 1 0.1372549 0.1254902 0.1529412
2 -2 1 0.1372549 0.1176471 0.1411765
3 -3 1 0.1294118 0.1137255 0.1176471
4 -4 1 0.1254902 0.1254902 0.1254902
5 -5 1 0.1254902 0.1176471 0.1294118
6 -6 1 0.1725490 0.1372549 0.1176471
現在我想用ggplot2繪制這個'圖像'。 我可以繪制一個特定的“通道”(紅色或綠色或藍色), 一個在使用時間:
ggplot(data=image.rgb, aes(
x=x, y=y,
col=g) #green for example
) + geom_point()
...在默認的ggplot2色標上
有沒有辦法指定可以從我指定的列中獲取確切的rgb值?
使用base
包中的plot
功能,我可以使用
with(image.rgb, plot(x, y, col = rgb(r,g,b), asp = 1, pch = "."))
但我希望能夠使用ggplot2來做到這一點
您必須添加scale_color_identity
才能使顏色“按原樣”采用:
ggplot(data=image.rgb, aes(x=x, y=y, col=rgb(r,g,b))) +
geom_point() +
scale_color_identity()
您提供的樣本數據給出了非常相似的顏色,因此所有點都顯示為黑色。 使用geom_tile
,不同的顏色更加明顯:
ggplot(data=image.rgb, aes(x=x, y=y, fill=rgb(r,g,b))) +
geom_tile() +
scale_fill_identity()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.