R plot raster colorscheme不是全范围

时间:2018-04-18 23:02:28

标签: r plot raster rastervis

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

library(RColorBrewer)
library(classInt)

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

enter image description here

现在我尝试包含分位数中断,我使用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%,其余颜色方案保持白色。

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:1)

修改:使用OP&amp;提供的数据来自rasterVis::levelplot

this answer解决方案
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 

enter image description here

您需要在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