4

I am trying to plot a raster with a defined colour scheme, which I take from the Rcolorbrewer package, so far no problem. The raster´s value range from 0 to 1 with no NA´s.

library(RColorBrewer)
library(classInt)

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

enter image description here

now I try to include quantile breaks, which I calculate using the ClassInt package

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)

incorrectly, plot() applies the colour scheme only to 50% of the value-range, the rest stays white.

enter image description here

what am I doing wrong?

Tung
  • 26,371
  • 7
  • 91
  • 115
a.urbanite
  • 75
  • 7

1 Answers1

1

Edit: using the data supplied by OP & rasterVis::levelplot solution from 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


You need to specify the number of colors in classIntervals then assign color codes to that classInterval object.

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

Source: Maps in R

Tung
  • 26,371
  • 7
  • 91
  • 115
  • the approach makes sense to me but the resulting plot doesnt https://drive.google.com/open?id=1PEmzaZTXl_AOa9KPXHtiWAEU825UYLR_ – a.urbanite Apr 19 '18 at 09:35
  • 1
    ah yes of course, sorry, here is the data. if you have a minute to look at it, or somebody else has an idea, i would be very thankful! https://drive.google.com/file/d/1mgH3p-_JuMGHzVBDmNfECz6SdkO15Fpj/view?usp=sharing – a.urbanite Apr 19 '18 at 22:21