【问题标题】:Correcting country names to make them match a different naming convention更正国家名称以使其匹配不同的命名约定
【发布时间】: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 个不同的数据集进行此过程,因此我不希望手动更正所有不匹配的国家/地区。

有没有办法解决这样的问题?我有几个想法,但不是一个实际的解决方案:

  1. 我可以进行模糊匹配,但这不适用于 USA -> United States。
  2. 我知道 countrycodes 包可以将国家/地区转换为 iso 代码等,但我认为它没有更正国家/地区名称的选项 (https://github.com/vincentarelbundock/countrycode)。
  3. 我可以以某种方式收集所有国家/地区的所有替代命名约定,然后对其进行模糊匹配。但我不知道从哪里获得替代名称,而且我不确定我是否能够再为这种情况编写模糊代码。

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: r ggplot2 country fuzzy-comparison


    【解决方案1】:

    一个选项是countrycode::countryname 来转换国家名称。

    注意:countrycode::countryname 会引发警告,因此它可能不适用于所有情况。但至少对我来说,失败的案例是相当异国情调的小国家或岛屿。

    library(ggplot2)
    library(countrycode)
    library(dplyr)
    library(tidyverse)
    
    worldmap <- 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, region = countryname(region), fill = ifelse(region %in% countryname(vec_AMIS_Market), "green", "lightgrey"))
    #> Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: Ascension Island, Azores, Barbuda, Canary Islands, Chagos Archipelago, Grenadines, Heard Island, Madeira Islands, Micronesia, Saba, Saint Martin, Siachen Glacier, Sint Eustatius, Virgin Islands
    
    # 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()
    

    【讨论】:

      猜你喜欢
      • 2016-12-08
      • 2016-02-07
      • 1970-01-01
      • 2018-02-10
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 2018-08-27
      相关资源
      最近更新 更多