【问题标题】:How can I plot a image with (x,y,r,g,b) coordinates using ggplot2?如何使用 ggplot2 绘制具有 (x,y,r,g,b) 坐标的图像?
【发布时间】:2013-10-10 15:37:24
【问题描述】:

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

> 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 来做到这一点

【问题讨论】:

  • 为了阐明数据帧的结构,对于每个 (x,y) - 在图像坐标范围内 - 数据帧中只有一个条目(不多也不少)
  • ggplot(data=image.rgb, aes(x=x, y=y, col=rgb(r,g,b))) + geom_point() 有什么问题?似乎在这里工作......
  • @juba rgb(r,g,b) 创建一个字符串值,该字符串值在传递给 col 参数时由 ggplot2 分配任意颜色,而不是其中指示的实际 rgb 颜色。
  • 啊,是的,你是对的。

标签: r image-processing plot ggplot2 rgb


【解决方案1】:

您必须添加 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()

【讨论】:

  • geom_raster() 是另一个需要考虑的选项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 2021-03-01
  • 2013-09-20
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
  • 2021-12-05
相关资源
最近更新 更多