需要帮助使用 rworldmap 为地块上的国家添加名称

Need help adding names to the countries on the plot with rworldmap

我只需要绘制出现在我的数据框中的国家名称,而不是所有世界国家的名称。有什么建议吗?

library(grid)
library(rworldmap)


q = c("ESP","AND","MCO","VCT","NLD", "LUX", "GBR", "LIE", "BEL","NLD", 
      "LUX","GBR","DNK","SWE","NOR","ATG","AUS", "BHS", "BHR","BRB",
      "BLZ","BTN","BRN","KHM","CAN","SWZ","GRD","JAM","JPN","KWT","JOR","LSO","MYS","MAR","NZL",
      "OMN","PNG","QAT","KNA","LCA","VCT","SAU","SLB","THA","TON","TUV","ARE")

DF = data.frame(q = c("ESP","AND","MCO","VCT","NLD", "LUX", "GBR", "LIE", "BEL","NLD", 
                      "LUX","GBR","DNK","SWE","NOR","ATG","AUS", "BHS", "BHR","BRB",
                      "BLZ","BTN","BRN","KHM","CAN","SWZ","GRD","JAM","JPN","KWT","JOR","LSO","MYS","MAR","NZL",
                      "OMN","PNG","QAT","KNA","LCA","VCT","SAU","SLB","THA","TON","TUV","ARE"), Assignment = ("Monarchies Worldwide"))
                

Map = joinCountryData2Map(DF, joinCode = "ISO3", nameJoinColumn ="q", mapResolution = "coarse" ) 

mapParams = mapCountryData(Map, nameColumnToPlot="Assignment", catMethod = "categorical",
               missingCountryCol = gray(.4))

``

这里有一些值得尝试的东西。创建可用于子集 coordinates(Map) 的国家/地区名称向量。您将获得选定的国家/地区名称,而不是地图的所有国家/地区名称。

library(rworldmap)

DF = data.frame(q = c("ESP", "CAN"), 
                Assignment = ("Monarchies Worldwide"))

country = c("Spain", "Canada")

Map = joinCountryData2Map(DF, 
                          joinCode = "ISO3", 
                          nameJoinColumn ="q", 
                          mapResolution = "coarse") 

mapParams = mapCountryData(Map, 
                           nameColumnToPlot="Assignment", 
                           catMethod = "categorical",
                           missingCountryCol = gray(.4))

country_coord <- data.frame(coordinates(Map))[country, ]

text(x = country_coord$X1, 
     y = country_coord$X2, 
     labels = row.names(country_coord))

地图