R--使用ggplot2-和栅格包进行地理参考的栅格图像的原始颜色 [英] R - original colours of georeferenced raster image using ggplot2- and raster-packages
问题描述
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屋!