我需要在 R 地图中绘制一些国家/地区,这些国家/地区是从数据集中提供的。国家必须根据排名值(1 到 5)着色,十六进制颜色值将自动分配。
library(maps)
library(geosphere)
library(magrittr)
x <- map("world", plot=FALSE)
paises <- (as.matrix(x$names))
# Set of countries to be colored
cntry <- c("Italy", "Portugal", "France", "Germany", "Spain")
# Set of colors for the selected countries
colset <- rainbow(length(cntry))
# Positions of the selected countries in the 'paises' vector
poss <- lapply(cntry, function(x) grep(x, paises))
# Vector of colors to be passed to 'map'
cols <- unlist(sapply(1:length(cntry), function(k) rep(colset[k], length(poss[[k]]))))
# Plot all european countries in gray
map("world", regions=paises, xlim=c(-25,46),ylim=c(34.5,71), bg="white",
interior = FALS