如何使用ggplot2绘制(x,y,r,g,b)坐标图像? [英] How can I plot a image with (x,y,r,g,b) coordinates using ggplot2?

查看:204
本文介绍了如何使用ggplot2绘制(x,y,r,g,b)坐标图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框 image.rgb ,其中我已经为图像的每个坐标加载了r,g,b值(使用 jpeg 重塑包)。它现在看起来像:

 > head(image.rgb)
yxrgb
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例如
)+ geom_point()

...使用默认的ggplot2色阶

有没有方法来指定确切的rgb值可以从我指定的列中获取?



使用函数我可以使用 $ b

 与(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()


I have a data frame image.rgb, into which I have loaded the r,g,b value for each coordinate of an image (using the jpeg and reshape packages). It now looks like:

> 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

Now I want to plot this 'image' using ggplot2. I can plot a specific 'channel' (red or green or blue) one at a time using:

ggplot(data=image.rgb, aes(
            x=x, y=y,
            col=g) #green for example
       ) + geom_point()

...on the default ggplot2 colour scale

Is there a way to specify that the exact rgb values can be taken from the columns I specify?

Using the plot function in the base package, I can use

with(image.rgb, plot(x, y, col = rgb(r,g,b), asp = 1, pch = "."))

But I'd like to be able to do this using ggplot2

解决方案

You must add scale_color_identity in order for colors to be taken "as is" :

ggplot(data=image.rgb, aes(x=x, y=y, col=rgb(r,g,b))) + 
    geom_point() + 
    scale_color_identity()

The sample data you provided give very similar colors, so all the points seem to be black. With geom_tile the different colors are a bit more visible :

ggplot(data=image.rgb, aes(x=x, y=y, fill=rgb(r,g,b))) +
    geom_tile() +
    scale_fill_identity()

这篇关于如何使用ggplot2绘制(x,y,r,g,b)坐标图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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