4

Good morning, I have spent a lot of time to figure out how can I add a country names directly on plot not like a part of legend, but like part of map. Im using package rworldmap, tried to use identifyCountries () - but it something for interaction (when a user clicks on the map), than I have found a such solution Administrative regions map of a country with ggmap and ggplot2 but it's for ggplot2, and too complicated. Im trying to do it with mapCountryData(). Hope for your help, thanks.

data <- data.frame(Country=c('Russia','Cyprus', 'Belize', 'Austria' ,'Virgin Islands', 
        'Italy','United States' ,'United Kingdom', 'Germany', 'France' ,'Poland' ,'Switzerland'),
Value=c(-0.310,-0.206,-0.300,-0.179,-0.196,-0.174,-0.105,-0.142,-0.082,-0.097,-0.027,0.052))

    library('rworldmap')

    pdf1 <- joinCountryData2Map(data, joinCode="NAME", nameJoinColumn="Country")

    mapCountryData(pdf1, nameColumnToPlot="Value", catMethod="pretty", 
    colourPalette='white2Black',addLegend='TRUE',mapTitle=NULL, mapRegion="Europe")
Andrew Reid
  • 37,021
  • 7
  • 64
  • 83
Dima Sukhorukov
  • 129
  • 4
  • 13

1 Answers1

7

You can try:

# get the coordinates for each country
country_coord<-data.frame(coordinates(pdf1),stringsAsFactors=F)
# label the countries
text(x=country_coord$X1,y=country_coord$X2,labels=row.names(country_coord))

passing these lines after your code, you'll get:

enter image description here

Cath
  • 23,906
  • 5
  • 52
  • 86
  • thank you, it took me a time to figure out how to deal with row.names in country_coord to subset the countries that I was needed, but finally I have made it, thank you for your help with coordinates, and the way to text it!! – Dima Sukhorukov Feb 12 '15 at 16:46
  • Good work. You can get the same result with less code if you wish. `text(pdf1, labels="NAME")` – Andy Feb 16 '15 at 12:49
  • @Andy, thanks, good to know ! Although, passing your code, I'm getting an error `cannot coerce type 'S4' to vector of type 'double'`... – Cath Feb 16 '15 at 12:55
  • 1
    @CathG Aha! Yes I've just realised that this only works if you also have the `raster` package loaded. – Andy Feb 16 '15 at 13:15