0

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

``
ozu
  • 5
  • 3
  • Does [this answer](https://stackoverflow.com/a/56623640/3460670) address what you're looking for? – Ben May 16 '21 at 16:07
  • unfortunately it doesn't, already tried! – ozu May 16 '21 at 16:25
  • https://stackoverflow.com/questions/56984456/is-there-a-way-to-add-labels-or-city-names-on-rworldmap I'd pretty much like to do this but only applied to countries rather than cities – ozu May 16 '21 at 16:33

1 Answers1

0

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

Ben
  • 28,684
  • 5
  • 23
  • 45