R--使用ggplot2-和栅格包进行地理参考的栅格图像的原始颜色 [英] R - original colours of georeferenced raster image using ggplot2- and raster-packages

查看:165
本文介绍了R--使用ggplot2-和栅格包进行地理参考的栅格图像的原始颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 colortable 。在由ggplot / ggplot2绘制的贴图中,将tifrel =nofollow>>>地理参考栅格图像<<< / strong>(tif文件) p>

由于没有找到更简单的解决方案,我从图例中访问 colortable / code> - 加载的栅格图像(对象)的属性 raster1 像这样:

  raster1 < - 栅格(paste(workingDir,/HUEK200_Durchlaessigkeit001_proj001.tif,sep =,collapse =))
raster1.pts< - rasterToPoints(raster1)
raster1.df< - data.frame(raster1.pts)
colTab< - attr(raster1,legend)@ colortable

好的,到目前为止这么好。现在,我只需要将 colortable 作为彩色标度应用于我现有的情节:

  + geom_tile(aes(x,y,fill = raster1.df [[3]]))
+ scale_fill_gradientn(values = 1:length( colTab),colors = colTab,guide = FALSE)
+ coord_fixed(ratio = 1)

不幸的是,这不能按预期工作。生成的图像不会显示白色旁边的任何颜色,以及未定义自定义值时经常出现的典型ggplot-gray。此刻,我有点无知,这里其实是错误的。我假定存储在 raster1.df [[3]] 中的底层band值是颜色表的索引。这可能是错误的。如果它是错误的,那么乐队值是如何与 colortable 连接的?即使我的假设是正确的:我给 scale_fill_gradientn()的参数应该仍然会产生一个更加丰富多彩的情节,不是吗?我检查了什么是唯一值:

  sort(unique(raster1.df [[3]]))

输出:

  [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 

显然,并非所有 colortable 的256个成员都使用,这提醒我颜色并不总是需要反映潜在的频带数据分布(特别是当包括多个频带时)。



我希望,我最后的想法并没有让你们对目标非常直观的事实感到困惑。

感谢您的帮助!

到所有的地理参考光栅图像,但可能差不多。

首先,我假设数据值确实代表颜色选择是错误的。空间光栅对象的 colortable 中有15种独特的颜色。但是,并非所有的都被使用(14和15)。好的,现在我知道了,我必须将我的值映射到相应的颜色,方法是 scale_fill_gradientn 可以理解。为此,我使用了我以前的初始代码片段,并定义了一个新变量 valTab ,它存储了给定乐队的所有唯一数据值:

  raster1 < - 光栅(paste(workingDir,/HUEK200_Durchlaessigkeit001_proj001.tif,sep =,collapse =))
raster1.pts< ; - rasterToPoints(raster1)
raster1.df< - data.frame(raster1.pts)
raster1.img< - melt(raster1)
colTab< - attr(raster1, colourable
names(colTab)< - 0:(length(colTab) - 1)
valTab< - sort(unique(raster1.df [[3]]))

请注意,如何定义 colTab - 这很快就会很重要。有了这个,我可以在绘图时自动将所有活动的颜色与它们各自的值相关联:

 (ggplot(data = raster1。 df)
+ geom_tile(aes(x,y,fill = raster1.df [[3]]))
+ scale_fill_gradientn(colors = colTab [as.character(valTab)])
+使用 valTab / code> - 成员作为对相应颜色索引的引用有助于始终仅挑选所需的颜色。我不知道在某些情况下是否需要定义 - scale_fill_gradientn()的参数。



我不确定由 raster()读取的栅格图像总是从开始定义它们的值, 0 。如果不是,则需要调整名称(colTab)< - 0:(长度(colTab) - 1)

我希望,这对未来有帮助。至少,我终于有了一个解决方案!


I would like to use the original colortable of a >>georeferenced raster image<< (tif-file) as coloured scale in a map plotted by ggplot/ggplot2.

Due to not finding an easier solution, I accessed the colortable-slot from the legend-attribute of the loaded raster image (object) raster1 like so:

raster1 <- raster(paste(workingDir, "/HUEK200_Durchlaessigkeit001_proj001.tif", sep="", collapse=""))
raster1.pts <- rasterToPoints(raster1)
raster1.df <- data.frame(raster1.pts)
colTab <- attr(raster1, "legend")@colortable

Ok, so far so good. Now I simply need to apply colortable as a colored scale to my existing plot:

(ggplot(data=raster1.df)
+ geom_tile(aes(x, y, fill=raster1.df[[3]]))
+ scale_fill_gradientn(values=1:length(colTab), colours=colTab, guide=FALSE)
+ coord_fixed(ratio=1)
)

Unfortunately, this does not work as expected. The resulting image does not show any colors beside white and the typical ggplot-grey which often appears when no custom values are defined. At the moment, I am a little clueless what is actually wrong here. I assumed that the underlying band values stored in raster1.df[[3]] are indices for the color table. This might be wrong. If it is wrong, then how are the band values connected with the colortable? And even if my assumption would be right: The parameters which I have given to scale_fill_gradientn() should still result in a more colorful plot, shouldn't they? I checked out what the unique values are:

sort(unique(raster1.df[[3]]))

This outputs:

 [1]  0  1  2  3  4  5  6  7  8  9 10 11 12

Apparently, not all of the 256 members of colortable are used which reminds me that the color does not always need to reflect the underlying band-data distribution (especially when including multiple bands).

I hope, my last thoughts didn't confuse you about the fact that the objective is quite straight forward.

Thank you for your help!

解决方案

Ok, I have found an answer which might not apply to every georeferenced raster image out there, but maybe almost.

First, my assumption that the data values do bot exactly represent the color selection was wrong. There are 15 unique colors in the colortable of the spatial raster object. However, not all of them are used (14 and 15). Ok, now I know, I have to map my values to the corresponding colors ina way that scale_fill_gradientn understands. For this I am using my previous initial code snippet and define a new variable valTab which stores all unique data values of the given band:

raster1 <- raster(paste(workingDir, "/HUEK200_Durchlaessigkeit001_proj001.tif", sep="", collapse=""))
raster1.pts <- rasterToPoints(raster1)
raster1.df <- data.frame(raster1.pts)
raster1.img <- melt(raster1)
colTab <- attr(raster1, "legend")@colortable
names(colTab) <- 0:(length(colTab) - 1)
valTab <- sort(unique(raster1.df[[3]]))

Notice, how index names are defined for colTab - this will be important soon. With this, I am able to automatically relate all active colors with their respective value while plotting:

(ggplot(data=raster1.df)
+ geom_tile(aes(x, y, fill=raster1.df[[3]]))
+ scale_fill_gradientn(colours=colTab[as.character(valTab)])
+ coord_fixed(ratio=1)
)

Using valTab-members as references to the corresponding color-indices helps to always pick only the colors which are needed. I don't know if defining the values-paramter of scale_fill_gradientn() is necessary in some cases.

I am not sure if the raster images read by raster() always define their values starting from 0. If not, names(colTab) <- 0:(length(colTab) - 1) needs to be adjusted.

I hope, this helps somebody in the future. At least, I finally have a solution!

这篇关于R--使用ggplot2-和栅格包进行地理参考的栅格图像的原始颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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