如何在R中创建具有相同色标的栅格图 [英] How can I create raster plots with the same colour scale in R

查看:108
本文介绍了如何在R中创建具有相同色标的栅格图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R中的栅格"包从栅格文件创建一些地图.我想创建比较栅格,并排显示多个地图.对此很重要,无论所有贴图中的值如何,所有贴图使用的色阶都相同.例如,如果地图1的值是0-1,地图2的值是0-0.5,则值为0.5的像元在两个地图上都应具有相同的颜色.

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.

例如:

  • 地图1的值从0到1
  • 地图2的值从0到0.5
  • 颜色从红色(最低)到绿色(最高)

我希望0.5的值在两个图中都具有相同的颜色(即黄色,介于红色和绿色之间).当前的行为是在地图1中为黄色,在地图2中为绿色.

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.

我找不到使这项工作成功的方法.我看不到任何方法来设置要与绘图功能一起使用的像素值范围. setMinMax()没有帮助(因为图"总是计算值).甚至尝试手动设置值(例如g1 @ data @ max<-10)也不起作用(在绘制时会忽略这些值).

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?

我最终使用的解决方案是:

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) ) 

推荐答案

由于image :: raster函数指定可以传递image :: base参数(并建议使用image :: base),因此不会您是否为所有对image :: raster的调用指定了相同的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:

 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.

这篇关于如何在R中创建具有相同色标的栅格图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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