【发布时间】:2021-11-26 15:58:42
【问题描述】:
我想用ggplot制作一张世界地图如下:
library(ggplot2)
library(countrycodes)
library(dplyr)
library(tidyverse)
worldmap_AMIS_Market <- map_data("world")
# Set colors
vec_AMIS_Market <- c("Canada", "China","United States of America", "Republic of Korea", "Russian Federation")
worldmap_AMIS_Market <- mutate(worldmap, fill = ifelse(region %in% vec_AMIS_Market, "green", "lightgrey"))
# Use scale_fiil_identity to set correct colors
ggplot(worldmap_AMIS_Market, aes(long, lat, fill = fill, group=group)) +
geom_polygon(colour="gray") + ggtitle("Map of World") +
ggtitle("Availability of AMIS Supply and Demand Data - Monthly") +
scale_fill_identity()
如您所见,美国没有以绿色点亮,因为在worldmap_AMIS_Market 数据中,美国写为USA,而向量使用United States of America。俄罗斯和韩国也是如此。由于我将对大约 50 个不同的数据集进行此过程,因此我不希望手动更正所有不匹配的国家/地区。
有没有办法解决这样的问题?我有几个想法,但不是一个实际的解决方案:
- 我可以进行模糊匹配,但这不适用于 USA -> United States。
- 我知道
countrycodes包可以将国家/地区转换为 iso 代码等,但我认为它没有更正国家/地区名称的选项 (https://github.com/vincentarelbundock/countrycode)。 - 我可以以某种方式收集所有国家/地区的所有替代命名约定,然后对其进行模糊匹配。但我不知道从哪里获得替代名称,而且我不确定我是否能够再为这种情况编写模糊代码。
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: r ggplot2 country fuzzy-comparison