Search code examples
rspatialshapefiler-spr-maptools

spplot issue with legend range and colors distribution


I have problem with correct color range on my plot and legend.

This is code which I use:

data.ch4 <- read.csv2("v42_CH4_1970_TOT.txt",skip = 3,stringsAsFactors = FALSE, header = F)
num_data <- data.frame(data.matrix(data.ch4))

library(maptools)
library(lattice)
library(png)

#map loading
map1 <- readShapePoly("CNTR_2014_03M_SH/Data/CNTR_RG_03M_2014.shp")
coordinates(num_data) <- ~V2+V1  
gridded(num_data) <- TRUE

#plotting
png(file="Map2.png",width=35,height=30,unit="cm", res=200, type = "cairo")

spplot(num_data["V3"], xlim=c(-5,35), ylim=c(35,70),
             sp.layout = list("sp.polygons",map1),contour=F)
dev.off()

Here is the file with data: https://www.sendspace.com/file/hjtatp (compressed beacuse normally it weights 57 mb)

Map from here (but map has secondary priority, it can be skipped)

This is how it looks like without any scale modifications: enter image description here

So everything is blue. Obviously there is to big scale distance, from min to max value. I would like to fix the scal, for e.g. last value would by "higher than x". I tried to do this like this:

enter image description here

So now this looks much better. This is how I did it:

#Fixed breakpoints (?)
at <- c(0e+0, 1.5e-5, 1.0e-4, 1.0e-3, 1.0e-2, 1.0e-1, 1.0e+0, 2.0e+0, 1.0e+1, 1.0e+2, 2.0e+2,5.0e+2)

spplot(num_data["V3"], xlim=c(-5,35), ylim=c(35,70),
             sp.layout = list("sp.polygons",map1),
             contour=F,
             at=at) #right there

So I added manually the at values (but not accurate scale). Everything looks much better but..

As you can see, scale on the right is not uniformly distributed. I cannot see any blue-purple colors, only orange and yellow.

Also some spots on the map are bright yellow (Germany area), because the values are highest here, but sadly there is no such color on the scale.

Probably I didn't do it properly. I don't know how to set the scale to looks good. I would like to have scale like this:

enter image description here

I achieved this by adding:

spplot(num_data["V3"], xlim=c(-5,35), ylim=c(35,70),
             sp.layout = list("sp.polygons",map1),
             contour=F,at=at,
             colorkey=list(at=seq(0, 400, 30))  #right there 
             )

But again, this is just fake scale, it won't work.

And the second fast question: How to add country contours on top of spplotted data? Because now contours are burried under the colorful data :c


Solution

  • The data converted to factor gives regular intervals to the legend. And you can change labels and its positions by colorkey = list(labels = list(at = ..., labels = ...)).

    [Edited; (I noticed that some values are over 500, sorry)]
    
     ## convert numeric to factor
    num_data@data$cutV3 <- cut(num_data@data$V3, breaks = c(at, Inf))   # I modified the breaks
    
    spplot(num_data["cutV3"], xlim=c(-5, 35), ylim=c(35, 70), 
       colorkey = list(height = 1, labels = list(at = seq(0.5, length(at) -0.5), labels = at)),
       sp.layout = list("sp.polygons", map1, first = F), contour = F)  # drawn after main plot
    

    enter image description here