2

我有一个情节的一部分wrld_simpl

library(maptools)
data(wrld_simpl)
cntr <- c('Denmark', 'Germany', 'Norway', 'Ireland', 'United Kingdom', 'France', 'Italy', 
         'Sweden', 'Finland', 'Spain', 'Portugal', 'Latvia', 'Estonia', 'Slovenia', 'Belgium',
         'Netherlands', 'Austria', 'Poland', 'Switzerland', 'Slovakia', 'Lithuania', 'Croatia', 
         'Czech Republic') 
my_map <- wrld_simpl[wrld_simpl$NAME %in% cntr,]
plot(my_map)

我想给一些国家上色,比如说德国为红色,丹麦为绿色。我该怎么做?我尝试了这样的事情,但它没有按照我想要的方式工作:

country_colors <- list(Germany='red', Denamark='green')
plot(my_map, col=unlist(country_colors[as.character(my_map$NAME)]))

谢谢!

4

1 回答 1

2

尝试使用与 my_map$NAME 长度相同的向量,而不是长度为 2 的向量:

country_colors <- setNames(rep("white", length(my_map$NAME)), my_map$NAME)
country_colors[c("Germany", "Denmark")] <- c("red", "green") 
plot(my_map, col = country_colors)
于 2014-12-28T12:26:37.050 回答