如何在 R 中创建具有相同色标的栅格图

发布于 2024-10-05 09:30:04 字数 729 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不寐倦长更 2024-10-12 09:30:04

现在简单的解决方案是使用 zlim 选项。

plot( d, col=rev( rainbow( 99, start=0,end=1 ) ),zlim=c(0,1) )

Easy solution now is to use the zlim option.

plot( d, col=rev( rainbow( 99, start=0,end=1 ) ),zlim=c(0,1) )
紙鸢 2024-10-12 09:30:04

由于 image::raster 函数指定可以传递 image::base 参数(并表明可能使用 image::base),您是否只需为所有对 image 的调用指定相同的 col= 和 Breaks= 参数::光栅?您确实需要让分隔符和 col 参数“同步”。颜色数量需要比中断数量少一。下面的示例基于经典的火山数据,第二个版本显示了如何从图像中排除一系列值:

 x <- 10*(1:nrow(volcano))
 y <- 10*(1:ncol(volcano))
 image(x, y, volcano, col = terrain.colors( length(seq(90, 200, by = 5))-1), axes = FALSE, breaks= seq(90, 200, by = 5) )
 axis(1, at = seq(100, 800, by = 100))
 axis(2, at = seq(100, 600, by = 100))
 box()
 title(main = "Maunga Whau Volcano", font.main = 4)



 x <- 10*(1:nrow(volcano))
 y <- 10*(1:ncol(volcano))
 image(x, y, volcano, col = terrain.colors( length(seq(150, 200, by = 5))-1), axes = FALSE, breaks= seq(150, 200, by = 5) )
 axis(1, at = seq(100, 800, by = 100))
 axis(2, at = seq(100, 600, by = 100))
 box()
 title(main = "Maunga Whau Volcano Restricted to elevations above 150", font.main = 4)

一个具体的示例将有助于这项工作。

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:

 x <- 10*(1:nrow(volcano))
 y <- 10*(1:ncol(volcano))
 image(x, y, volcano, col = terrain.colors( length(seq(90, 200, by = 5))-1), axes = FALSE, breaks= seq(90, 200, by = 5) )
 axis(1, at = seq(100, 800, by = 100))
 axis(2, at = seq(100, 600, by = 100))
 box()
 title(main = "Maunga Whau Volcano", font.main = 4)



 x <- 10*(1:nrow(volcano))
 y <- 10*(1:ncol(volcano))
 image(x, y, volcano, col = terrain.colors( length(seq(150, 200, by = 5))-1), axes = FALSE, breaks= seq(150, 200, by = 5) )
 axis(1, at = seq(100, 800, by = 100))
 axis(2, at = seq(100, 600, by = 100))
 box()
 title(main = "Maunga Whau Volcano Restricted to elevations above 150", font.main = 4)

A specific example would aid this effort.

じее 2024-10-12 09:30:04

添加为回应@Tomas 的答案

我最终使用的答案是:

plot( d, col=rev( rainbow( 99, start=0,end=1 ) ), 
    breaks=seq(min(minValue( d )),max(maxValue(d)),length.out=100) ) 

Added as an answer in response to @Tomas

The answer 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) ) 
好听的两个字的网名 2024-10-12 09:30:04

在“raster”中还有更多工作要做,但这里有一个技巧:

 library(raster)
 r1 <- r2 <- r3 <- raster(ncol=10, nrow=10)
 r1[] <- runif(ncell(r1))
 r2[] <- runif(ncell(r2)) / 2
 r3[] <- runif(ncell(r3)) * 1.5
 r3 <- min(r3, 1)
 s <- stack(r1, r2, r3)


 brk <- c(0, 0.25, 0.5, 0.75, 1)
 par(mfrow=c(1,3))
 plot(r1, breaks=brk, col=rainbow(4), legend=F)
 plot(r1, breaks=brk, col=rainbow(4), legend.only=T, box=F)
 plot(r2, breaks=brk, col=rainbow(4), legend=F)
 plot(r1, breaks=brk, col=rainbow(4), legend.only=T, box=F)
 plot(r3, breaks=brk, col=rainbow(4), legend=F)
 plot(r1, breaks=brk, col=rainbow(4), legend.only=T, box=F)

您还可以使用 spplot 函数(sp 包)

 s <- stack(r1, r2, r3) 
 sp <- as(s, 'SpatialGridDataFrame')
 spplot(sp)

您还可以将值发送到 ggplot(搜索 r-sig-geo 档案以获取示例)
如果您的 RasterLayer 链接到一个非常大的文件,您可能首先这样做,然后再转到 ggplot

r <- sampleRegular(r, size=100000, asRaster=TRUE) 

,然后也许

m <- as.matrix(r)

There is more work to be done here in 'raster' but here is a hack:

 library(raster)
 r1 <- r2 <- r3 <- raster(ncol=10, nrow=10)
 r1[] <- runif(ncell(r1))
 r2[] <- runif(ncell(r2)) / 2
 r3[] <- runif(ncell(r3)) * 1.5
 r3 <- min(r3, 1)
 s <- stack(r1, r2, r3)


 brk <- c(0, 0.25, 0.5, 0.75, 1)
 par(mfrow=c(1,3))
 plot(r1, breaks=brk, col=rainbow(4), legend=F)
 plot(r1, breaks=brk, col=rainbow(4), legend.only=T, box=F)
 plot(r2, breaks=brk, col=rainbow(4), legend=F)
 plot(r1, breaks=brk, col=rainbow(4), legend.only=T, box=F)
 plot(r3, breaks=brk, col=rainbow(4), legend=F)
 plot(r1, breaks=brk, col=rainbow(4), legend.only=T, box=F)

You can also use the spplot function (sp package)

 s <- stack(r1, r2, r3) 
 sp <- as(s, 'SpatialGridDataFrame')
 spplot(sp)

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

r <- sampleRegular(r, size=100000, asRaster=TRUE) 

and then perhaps

m <- as.matrix(r)
爱的故事 2024-10-12 09:30:04

它对我不起作用。我使用这个脚本来分割色标并根据我的数据选择更合适的一个:

plot(d, col=rev(heat.colors(8, alpha = 1)), breaks = seq(0, 0.40, by = 0.05))

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:

plot(d, col=rev(heat.colors(8, alpha = 1)), breaks = seq(0, 0.40, by = 0.05))
蓝天白云 2024-10-12 09:30:04

一个通常应该有效的非常简单的解决方案(例如,使用栅格包中的“绘图”功能)是设置“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))
...

冬天旳寂寞 2024-10-12 09:30:04

尝试 rasterVis::levelplot(x)
其中 x 是光栅文件的堆栈

Try rasterVis::levelplot(x)
where x is a stack of your raster files

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文