Search code examples
rheatmaplatticer-spviridis

Is there a way to add the viridis color palette to a spplot having these requirements?


I need to make a spplot for a college project. The teacher has given us instructions on how to create the spplot that shows the map. However, he gives the code with default colors and I need that the colors are ones belonging to the viridis palette (I don´t care which ones as long as they belong to that palette).

Here are the several approaches I´ve tried with no success:

#This is the compulsory code we need to use:
library(sp)
mapa = readRDS("gadm36_ESP_2_sp.rds")
names(mapa)
mapa$NAME_1

Com_Val<-mapa[mapa$NAME_1=="Comunidad Valenciana",]
Com_Val$cantidad<-c(29,10,65)    

# These numbers on the upper line are the ones who give color to the plot,
# you can choose the numbers you want to

spplot(Com_Val,"cantidad")

this is the plot I obtain with this code

#Now, the different approaches I´ve tried with no succes:

Hex color codes

Com_Val$cantidad<-c("#44015499","#3B528B99","#21908C99")

Error I obtain:

Error in seq.default(zrng[1], zrng[2], length.out = cuts + 2) : 
  'from' must be a finite number
In addition: Warning messages:
1: In extend.limits(range(as.numeric(z), finite = TRUE)) :
  NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

RGB color codes

Com_Val$cantidad<-c(rgb(255, 87, 51),rgb(59, 82, 139),rgb(33, 144, 140))

Error I obtain:

> Com_Val$cantidad<-c(rgb(255, 87, 51),rgb(59, 82, 139),rgb(33, 144, 140))
Error in rgb(255, 87, 51) : color intensity 51, not in [0,1]
> spplot(Com_Val,"cantidad")
Error in seq.default(zrng[1], zrng[2], length.out = cuts + 2) : 
  'from' must be a finite number
In addition: Warning messages:
1: In extend.limits(range(as.numeric(z), finite = TRUE)) :
  NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
> 

Adding scale_color_viridis or scale_fill_viridis

After

Com_Val$cantidad<-c(29,10,65)

spplot(Com_Val,"cantidad")+
  scale_color_viridis(alpha=1,discrete=TRUE, option="D")

Error I obtain:

NULL

(With scale_fill_viridis I obtain the same error).

How can I change the palette, maintaining the compulsory code structure from the beginning?Or, in case it is impossible to change the palette to viridis without altering the code that tells you how to get the picture of the map, how can I change the palette using another code?


Solution

  • You can use the viridis package to specify a vector of colours. These are passed to the col.regions argument of spplot

    library(sp)
    
    mapa <- readRDS("gadm36_ESP_2_sp.rds")
    
    Com_Val <- mapa[mapa$NAME_1 == "Comunidad Valenciana", ]
    Com_Val$cantidad <- c(29, 10, 65)
    
    spplot(Com_Val, "cantidad", col.regions = viridis::plasma(100))
    

    enter image description here


    Data source

    url <- "https://geodata.ucdavis.edu/gadm/gadm3.6/Rsp/gadm36_ESP_2_sp.rds"
    download.file(url, "gadm36_ESP_2_sp.rds")