我有一个包含两列的数据框,例如下面的
df
,我想使用maps::map()
来创建一个全局等值线图。我希望将颜色分配给 df
中存在的那些国家,剩下的只是空的无色多边形。但是结果和我预想的大相径庭。我尝试使用以下脚本:
另外,为什么世界地图质量这么低?有没有办法让它变得更好?
df <- tribble(
~Country, ~Value,
"Jordan", 35,
"USA", 84,
"Turkey", 65,
"Pakistan", 43
)
percent_map <- function(var, color, legend.title, min = 0, max = 100) {
# generate vector of fill colors for map
shades <- colorRampPalette(c("white", color))(100)
# constrain gradient to percents that occur between min and max
var <- pmax(var, min)
var <- pmin(var, max)
percents <- as.integer(cut(var, 100,
include.lowest = TRUE, ordered = TRUE))
fills <- shades[percents]
# plot choropleth map
maps::map("world", fill = TRUE, col = fills,
resolution = 0,
myborder = 0, mar = c(0,0,0,0))
# overlay state borders
maps::map("world", col = "white", fill = FALSE, add = TRUE,
lty = 1, lwd = 1,
myborder = 0, mar = c(0,0,0,0))
}
percent_map(var = df$Value,
color = "darkgreen",
legend.title = "Global Map for Value",
min = 0, max = 100
)