【问题标题】:Using a raster attribute from a multi-attribute raster for colour levels in a plot in R使用来自多属性栅格的栅格属性用于 R 中绘图中的颜色级别
【发布时间】:2015-04-21 11:09:26
【问题描述】:

我有一个具有大量属性的raster 对象,我想在 R 中绘制空间数据并按某个属性对其进行颜色编码。我无法弄清楚如何使用特定属性的信息来实现这一点。到目前为止,我已经使用factorValues() 成功提取了选择属性,但我无法确定现在如何将此信息合并到plot() 函数中。我尝试使用光栅包文档中提到的ratify()level() 函数,但我不明白简化的在线示例如何适用于具有多个属性的光栅。

任何关于如何实现这一点的建议将不胜感激。

# read in shapefile
shp = readOGR(".", "grid") 

#convert to raster
r = raster(extent(shp))
res(r) = c(1,0.5) 
ra = rasterize(shp, r)

#crop raster to desired extent
rcrop = crop(ra, extent(-12, 2, 29, 51))

# extract attribute value of interest 
f = factorValues(rcrop, 1:420, layer=1, att=17, append.names=FALSE)
# here there are 420 cells in the raster and I am interested in plotting values of attribute 17 of the raster (this is currently a numeric attribute, not a factor)

#extra code to set attribute as the level to use for plotting colours???
rcrop = ratify(rcrop)
rat = levels(rcrop)[[1]] #this just extras row IDs..not what I want
#…

### plot: I want to plot the grid using 7 colours (I would ideally like to specify the breaks myself)
require(RColorBrewer)
cols = brewer.pal(7,"YlGnBu")
#set breaks 
brks = seq(min(minValue(rcrop)),max(maxValue(rcrop),7))
#plot          
plot(rcrop, breaks=brks, col=cols, axis.arg=arg)

【问题讨论】:

    标签: r plot attributes raster


    【解决方案1】:

    以下内容相当老套(对于大型栅格可能表现不佳),但我不确定是否有办法将col.regions 链接到指定属性。

    rasterVis::levelplot 在标记与因子栅格对应的颜色渐变方面做得很好,虽然它提供了一个 att 参数允许您指定您感兴趣的属性,但这似乎只会修改渐变的标签.栅格像元值控制色带如何映射到栅格,因此在我看来,我们需要自己修改像元值。也许@OscarPerpiñán 会在这里证明我错了:)

    我们可以创建一个简单的函数来用我们想要的任何属性替换原始单元格值:

    switch_att <- function(r, att) {
      r[] <- levels(r)[[1]][values(r), att]
      r
    }
    

    让我们从Natural Earth 下载并导入一个小的示例多边形数据集:

    library(rasterVis)
    library(rgdal)
    require(RColorBrewer)
    
    download.file(file.path('http://www.naturalearthdata.com',
                            'http//www.naturalearthdata.com/download/110m/cultural',
                            'ne_110m_admin_0_countries.zip'),
                  f <- tempfile())
    unzip(f, exdir=tempdir())
    shp <- readOGR(tempdir(), 'ne_110m_admin_0_countries')
    

    rasterize矢量数据:

    r <- rasterize(shp, raster(raster(extent(shp), res=c(1, 1))))
    

    并使用levelplot 创建一些图:

    levelplot(switch_att(r, 'continent'), col.regions=brewer.pal(8, 'Set2')) +
        layer(sp.polygons(shp, lwd=0.5))
    

    levelplot(switch_att(r, 'economy'), par.settings=BuRdTheme) + 
      layer(sp.polygons(shp, lwd=0.5))
    


    编辑

    随着 Oscar 对 rasterVis 的更新,不再需要上面的 switch_att hack。

    devtools::install_github('oscarperpinan/rastervis')
    levelplot(r, att='continent', col.regions=brewer.pal(8, 'Set2')) + 
      layer(sp.polygons(shp, lwd=0.5))
    

    将产生与上面第一个相同的数字。

    【讨论】:

    • 你的假设是对的。我在levelplot 代码中有committed a modification 以改善其行为。有了它,您就不需要switch_att 技巧了。顺便说一句,很好的例子。
    • @jbaums:谢谢,这太棒了!正是我想要的。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2018-05-15
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2013-02-27
    相关资源
    最近更新 更多