我需要在仅中美洲的裁剪世界地图中为哥斯达黎加上色。
我已经编写的代码包含在下面......我希望能够使用 ggplot2 和 sf 库来完成它。
library("ggplot2")
theme_set(theme_bw())
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")
library("maps")
world <- ne_countries(scale='medium',returnclass = 'sf')
class(world)
(CentralAmerica <- ggplot(data = world) +
geom_sf() +
coord_sf(xlim = c(-60, -120), ylim = c(5, 35), expand = FALSE) +
scale_fill_viridis_d(option = "plasma") +
theme(panel.background = element_rect(fill = "azure"),
panel.border = element_rect(fill = NA)))
最佳答案
一种更有效的方法是在以下内容中包含 if()
或 ifelse()
语句:
geom_sf(fill = ifelse(world$geounit == "Costa Rica", 'red', 'blue'))
关于r - 如何在 map 上的特定国家着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56672192/