Search code examples
rplotchoroplethgwmodel

How to edit the palette bar in the R function spplot?


I am using the R package GWmodel. While plotting the output of the GWR I found myself unable to edit the side bar in the ssplot function.

The few lines of code I'm using(data is already in package) will be sufficient. You only need to install the GWmodel library:

library(GWmodel)
data("DubVoter")

bw.gwr.1 <- bw.gwr(GenEl2004 ~ DiffAdd + LARent + SC1 + Unempl + LowEduc + Age18_24 + Age25_44 + Age45_64, data = Dub.voter, approach = "CV", kernel = "gaussian", adaptive = FALSE)

gwr.res <- gwr.basic(GenEl2004 ~ DiffAdd + LARent + SC1 + Unempl + LowEduc + Age18_24 + Age25_44 + Age45_64, data = Dub.voter, bw = bw.gwr.1,  kernel = "gaussian", adaptive = FALSE, F123.test = TRUE)



Greens <- c("#238B45","#74C476","#BAE4B3","#EDF8E9")

spplot(gwr.res$SDF, "Unempl", cuts = 4, at = c(fivenum(gwr.res$SDF$Unempl)), col.regions = Greens)

the outcome looks like:

enter image description here

As you can see in the spplot function, the parameter at is given the value c(fivenum(gwr.res$SDF$Unempl)) which would correspond to min, 1st quartile, median, 3rd quartile, max of the values in gwr.res$SDF$Unempl. Looking at the graph, we can see that the colors respect this partition into 4 classes (using the arguments cuts = 4 at = c(fivenum(gwr.res$SDF$Unempl))).

What I would like to do is to edit the side bar with the palette and add replace 6,4,2,0,-2 with the values in c(fivenum(gwr.res$SDF$Unempl)) which are:

-2.427, -0.916, -0. 6.706, -0.494, 7.566


Solution

  • That's the code that works:

    labelat = fivenum(gwr.res$SDF$Unempl)
    labeltext = labelat 
    
    
    spplot(gwr.res$SDF, "Unempl", cuts = 4, at = c(fivenum(gwr.res$SDF$Unempl)), col.regions = Greens,
    colorkey=list(width=0.3,      
                  space="right", 
                  tick.number=5, 
                  labels=list(  
                    at=labelat, 
                    labels=labeltext )))
    

    enter image description here

    note that values on the side bar are different from -2.427, -0.916, -0. 6.706, -0.494, 7.566 because data is different.