use*_*594 4 rgb plot r image-processing 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
Run Code Online (Sandbox Code Playgroud)
现在我想用ggplot2绘制这个'图像'.我可以绘制一个特定的"通道"(红色或绿色或蓝色),一个在使用时间:
ggplot(data=image.rgb, aes(
x=x, y=y,
col=g) #green for example
) + geom_point()
Run Code Online (Sandbox Code Playgroud)
...在默认的ggplot2色标上
有没有办法指定可以从我指定的列中获取确切的rgb值?
使用包中的plot
功能base
,我可以使用
with(image.rgb, plot(x, y, col = rgb(r,g,b), asp = 1, pch = "."))
Run Code Online (Sandbox Code Playgroud)
但我希望能够使用ggplot2来做到这一点
您必须添加scale_color_identity
以便"按原样"采用颜色:
ggplot(data=image.rgb, aes(x=x, y=y, col=rgb(r,g,b))) +
geom_point() +
scale_color_identity()
Run Code Online (Sandbox Code Playgroud)
您提供的样本数据给出了非常相似的颜色,因此所有点都显示为黑色.随着geom_tile
不同的颜色更加明显:
ggplot(data=image.rgb, aes(x=x, y=y, fill=rgb(r,g,b))) +
geom_tile() +
scale_fill_identity()
Run Code Online (Sandbox Code Playgroud)