如何在 R 中创建具有相同色标的栅格图
我正在使用 R 中的“raster”包从栅格文件创建一些地图。我想创建比较栅格,并排显示多个地图。为此,重要的是,无论每个地图中的值如何,所有地图使用的色标都相同。例如,如果地图 1 的值介于 0-1 之间,而地图 2 的值介于 0-0.5 之间,则值为 0.5 的单元格在两个地图上应具有相同的颜色。
例如:
- 地图 1 的值从 0 到 1
- 地图 2 的值从 0 到 0.5
- 颜色从红色(最低)到绿色(最高)
我希望值 0.5 在两个地图中具有相同的颜色(即黄色) ,介于红色和绿色之间)。目前的行为是,它在地图 1 中为黄色,在地图 2 中为绿色。
我找不到一种方法来实现这一点。我看不到任何方法来设置与绘图函数一起使用的像素值范围。 setMinMax() 没有帮助(因为“plot”总是计算值)。即使尝试手动设置值(例如 g1@data@max <- 10)也不起作用(绘图时将忽略这些值)。
最后,制作一堆地图(可能期望以相同的色标绘制所有内容)也不起作用 - 每个地图仍然有自己的色标。
关于如何做到这一点有什么想法吗?
编辑:
我最终使用的解决方案是:
plot( d, col=rev( rainbow( 99, start=0,end=1 ) ), breaks=seq(min(minValue( d )),max(maxValue(d)),length.out=100) )
I'm creating some maps from raster files using the "raster" package in R. I'd like to create comparison rasters, showing several maps side by side. It's important for this that the colour scales used are the same for all maps, regardless of the values in each map. For example, if map 1 has values from 0-1, and map 2 has values from 0-0.5, cells with a value of 0.5 should have the same colour on both maps.
For example:
- map 1 has values from 0 to 1
- map 2 has values from 0 to 0.5
- the colour goes from red (lowest) to green (highest)
I would like a value of 0.5 to have the same colour in both maps (i.e. yellow, as halfway between red and green). The current behaviour is that it is yellow in map 1, and green in map 2.
I can't find a way to make this work. I can't see any way to set the range of pixel values to use with the plotting function. setMinMax() doesn't help (as 'plot' always calculates the values). Even trying to set the values by hand (e.g. g1@data@max <- 10) doesn't work (these are ignored when plotting).
Finally, making a stack of the maps (which might be expected to plot everything on the same colour scale) doesn't work either - each map still has it's own colour scale.
Any thoughts on how to do this?
EDIT:
The solution I ended up using is:
plot( d, col=rev( rainbow( 99, start=0,end=1 ) ), breaks=seq(min(minValue( d )),max(maxValue(d)),length.out=100) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
现在简单的解决方案是使用 zlim 选项。
Easy solution now is to use the zlim option.
由于 image::raster 函数指定可以传递 image::base 参数(并表明可能使用 image::base),您是否只需为所有对 image 的调用指定相同的 col= 和 Breaks= 参数::光栅?您确实需要让分隔符和 col 参数“同步”。颜色数量需要比中断数量少一。下面的示例基于经典的火山数据,第二个版本显示了如何从图像中排除一系列值:
一个具体的示例将有助于这项工作。
Since the image::raster function specifies that the image::base arguments can be passed (and suggests that image::base is probably used), wouldn't you just specify the same col= and breaks= arguments to all calls to image::raster? You do need to get the breaks and the col arguments "in sync". The number of colors needs to be one less than the number of breaks. The example below is based on the classic volcano data and the second version shows how a range of values can be excluded from an image:
A specific example would aid this effort.
添加为回应@Tomas 的答案
我最终使用的答案是:
Added as an answer in response to @Tomas
The answer I ended up using is:
在“raster”中还有更多工作要做,但这里有一个技巧:
您还可以使用 spplot 函数(sp 包)
您还可以将值发送到 ggplot(搜索 r-sig-geo 档案以获取示例)
如果您的 RasterLayer 链接到一个非常大的文件,您可能首先这样做,然后再转到 ggplot
,然后也许
There is more work to be done here in 'raster' but here is a hack:
You can also use the spplot function (sp package)
You can also send the values to ggplot (search the r-sig-geo archives for examples)
If your RasterLayer links to a very large file, you might first do, before going to ggplot
and then perhaps
它对我不起作用。我使用这个脚本来分割色标并根据我的数据选择更合适的一个:
It did not work for me. I used this script to split the color scale and select the one more suitable according to my data:
一个通常应该有效的非常简单的解决方案(例如,使用栅格包中的“绘图”功能)是设置“z 轴”限制(控制颜色和颜色图例)。
例如你可以这样做:
plot(d, zlim=c(0,1))
其中 d 是堆叠栅格对象。或者,如果你有一堆单独的栅格 d1、d2、d2...,你可以这样做:
绘图(d1,zlim=c(0,1))
绘图(d2,zlim=c(0,1))
绘图(d3,zlim=c(0,1))
...
A pretty simple solution that should usually work (e.g. with the "plot" function in the raster package) is to set "z axis" limits (which control the colors and the color legend).
E.g. you can do something like:
plot(d, zlim=c(0,1))
where d is a stacked raster object. Or, if you have a bunch of separate rasters d1, d2, d2..., you can just do:
plot(d1, zlim=c(0,1))
plot(d2, zlim=c(0,1))
plot(d3, zlim=c(0,1))
...
尝试 rasterVis::levelplot(x)
其中 x 是光栅文件的堆栈
Try rasterVis::levelplot(x)
where x is a stack of your raster files