【问题标题】:R plot raster colorscheme not full rangeR plot raster colorscheme不是全范围
【发布时间】:2018-09-29 07:39:45
【问题描述】:

我正在尝试使用从 Rcolorbrewer 包中获取的已定义配色方案绘制栅格,到目前为止没有问题。栅格的值范围从 0 到 1,没有 NA。

library(RColorBrewer)
library(classInt)

pal <- brewer.pal(n=50, name = "RdYlGn")
plot(rw_start_goal_stan, col=pal)

现在我尝试包含分位数中断,我使用 ClassInt 包计算

library(RColorBrewer)
library(classInt)

pal <- brewer.pal(n=50, name = "RdYlGn")
breaks.qt <- classIntervals(rw_start_goal_stan@data@values, style = "quantile")
plot(rw_start_goal_stan, breaks = breaks.qt$brks, col=pal)

错误地,plot() 仅将配色方案应用于值范围的 50%,其余部分保持白色。

我做错了什么?

【问题讨论】:

    标签: r plot raster rastervis


    【解决方案1】:

    编辑:使用来自this answer的OP和rasterVis::levelplot解决方案提供的数据

    library(raster)
    library(rasterVis)
    library(classInt)
    
    plotVar <- raster("LCPs_standartized.tif")
    
    nColor <- 50
    break1 <- classIntervals(plotVar[!is.na(plotVar)], 
                             n = nColor, style = "quantile")
    
    lvp <- levelplot(plotVar, 
                     col.regions = colorRampPalette(brewer.pal(9, 'RdYlGn')), 
                     at = break1$brks, margin = FALSE)
    lvp 
    


    您需要指定classIntervals 中的颜色数量,然后将颜色代码分配给该classInterval 对象。

    library(RColorBrewer)
    library(classInt)
    
    plotVar <- rw_start_goal_stan@data@values
    nColor <- 50
    plotColor <- brewer.pal(nColor, name = "RdYlGn")
    
    # equal-frequency class intervals
    class <- classIntervals(plotVar, nColor, style = "quantile")
    # assign colors to classes from classInterval object
    colorCode <- findColours(class, plotColor)
    
    # plot
    plot(rw_start_goal_stan)
    plot(rw_start_goal_stan, col = colorCode, add = TRUE)
    
    # specify the location of the legend, change -117 & 44 to numbers that fit your data
    legend(-117, 44, legend = names(attr(colorCode, "table")),
      fill = attr(colorCode, "palette"), cex = 0.8, bty = "n")
    

    来源:Maps in R

    【讨论】:

    猜你喜欢
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 2020-06-17
    • 1970-01-01
    • 2013-05-27
    • 2021-07-17
    相关资源
    最近更新 更多