rrworldmap

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


I need to plot the names of the countries that are present in my dataframe only, not all world countries. Any suggestions?

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))

``

Solution

  • Here is something to try. Create a vector of country names that can be used to subset coordinates(Map). You would get selected country names, not all country names for the 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))
    

    Map

    map with selected country names