有没有办法将viridis调色板添加到具有这些要求的spplot中?

lyr7nygr  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(197)

我需要为一个大学项目做一个spplot。老师已经给了我们如何创建显示Map的spplot的指导。然而,他给出了带有默认颜色的代码,我需要这些颜色属于viridis调色板(我不在乎哪些颜色,只要它们属于那个调色板)。
以下是我尝试过但没有成功的几种方法:

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

这是我用这段代码得到的图

现在,我尝试过的不同方法都没有成功

十六进制颜色代码

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

我获得的错误:

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颜色代码

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

我获得的错误:

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

添加绿色缩放颜色或绿色缩放填充

之后

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

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

我获得的错误:

(With scale_fill_viridis我得到了相同的误差)。
如何更改调色板,从一开始就保持强制代码结构?或者,如果不更改告诉您如何获得Map图片的代码就无法将调色板更改为绿色,如何使用另一个代码更改调色板?

ctrmrzij

ctrmrzij1#

你可以使用viridis包来指定一个颜色矢量。这些矢量被传递给spplotcol.regions参数

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

数据来源

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

相关问题