I am creating a world map with rworldmap and adding the country names using the text function. However, the text labels overlap. I tried the adj and pos parameters, but with no luck thus far. Any tips?
library(rworldmap)
DF = data.frame(q = c("VAT","ESP","CAN","AND","MCO","VCT",
"NLD", "LUX", "GBR", "LIE", "BEL", "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","SAU",
"SLB","THA","TON","TUV","ARE" ),
Assignment = ("Monarchies Worldwide"))
country = c("Vatican","Spain","Monaco","Canada","Andorra","Saint Vincent and the Grenadines",
"Netherlands", "Luxembourg","United Kingdom", "Liechtenstein","Belgium","Denmark",
"Sweden","Norway","Anitgua and Barbuda","Australia", "Bahamas", "Bahrain","Brunei Darussalam",
"Belize","Bhutan","Cambodia","Swaziland","Grenada","Jamaica","Japan","Kuwait","Jordan",
"Lesotho","Malaysia","Morocco","New Zealand","Oman","Papua New Guinea","Qatar","Saint Kitts and Nevis",
"Saint Lucia","Saudi Arabia","Solomon Islands","Thailand","Tonga","Tuvalu","United Arab Emirates")
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),
adj = NULL, pos = 3, offset = 0, vfont = NULL,
cex = 0.3, col = "blue", font = 5)
Answer
The base
textfunction does not have this functionality. You'll likely have to rely on additional packages to achieve what you want:ggplot2::ggplotand useggrepel::geom_text_repelorggrepel::geom_label_repel.1.
basicPlotteR::addTextLabelsGiven some settings, it will displace the text, and use lines to indicate to which country the text belongs.
2.
ggrepel::geom_text_repelThis will do the same thing, but with more customization options. Note that
geom_text_repel(andgeom_label_repel) will remove labels if they cannot be plotted within your requirements.3.
ggrepel::geom_label_repelThis will function like
geom_text_repel(also in removing labels that cannot fit the requirements), but the text will have a nice box around it.