首页 > 解决方案 > 使用 ggplot 复制空间点数据框 RGB 值的 R plot()

问题描述

我正在尝试使用 ggplot 复制空间点数据框的 plot() ,该数据框具有 rgb 值来绘制颜色栅格。这个问题似乎很基本,我经常使用 ggplot ......任何帮助将不胜感激。

空间点数据框amre_map如下所示:

> head(amre_map)
  red green blue
1 150    61  152
2 248    55   83
3 220    66   61
4 118   127   73
5 124   126   71
6 188    74   71

> class(amre_map)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

> dim(amre_map)
[1] 100000      3

我要复制的绘图的 R 代码是:

> par(mar=c(0,0,0,0))
> plot(amre_map,col=rgb(r,g,b,max=255),pch=15)
> lines(breeding.shp)

gf_plot

我开始工作的最接近的 ggplot 代码如下。我转换amre_map.df为数据框:

> ggplot(amre_map.df, aes(x=x, y=y,fill = rgb(red,green,blue, maxColorValue = 255))) + 
theme_bw() +
geom_tile() + 
coord_equal() + 
scale_fill_identity()

gf_ggplot

我也尝试过geom_raster(),但是计算机在它上面挣扎了很长时间,除了警告之外没有产生任何东西

> ggplot() +
geom_raster(data = amre_map.df, 
  aes(x = x, y = y, fill = rgb(red, green ,blue, maxColorValue = 255)

Warning messages:
1: In f(...) :
Raster pixels are placed at uneven horizontal intervals and will be shifted. Consider using geom_tile() instead.

在此先感谢您的帮助!

标签: rggplot2rgb

解决方案


推荐阅读