Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add country names to map in ggplot

I try to make a map in ggplot of South America and the number of cases.
I would like to add the name of the countries.

However, it is difficult to add the number of cases in the legend and the names of the countries.

library(ggplot2)
library(dplyr)

sa.countries <- c('Argentina', 'Bolivia', 'Brazil',  
               'Colombia', 'Chile','Ecuador','French Guiana','Guyana', 
               'Paraguay', 'Peru', 'Suriname', 
               'Trinidad and Tobago', 'Uruguay', 'Venezuela')

countries.maps <- map_data("world", region = sa.countries)

country.cases <- tribble(~region, ~papers,
                          'Argentina', 33, 'Bolivia', 8, 'Brazil', 242,
                          'Colombia', 41, 'Chile', 9, 'Ecuador', 44,
                          'French Guiana', 3, 'Guyana', 0, 'Paraguay', 1,
                          'Peru', 8, 'Suriname', 0, 'Trinidad and Tobago', 2,
                          'Uruguay', 0, 'Venezuela', 7)

df.country <- left_join(countries.maps, country.cases, by="region")


ggplot(df.country, aes(long, lat, group = group))+
  geom_polygon(aes(fill = papers ), color = "white") +
  scale_fill_viridis_c(name='# cases', option = "C") +
  coord_equal() +
  theme_bw()

Here is an example of the map as it should be

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Thanks in advance.

>Solution :

You may try

dff <- df.country %>%
  group_by(region) %>%
  summarize(long = mean(long, na.rm = T), lat = mean(lat, na.rm = T), group = group)
ggplot(df.country, aes(long, lat, group = group))+
  geom_polygon(aes(fill = papers ), color = "white") +
  scale_fill_viridis_c(name='# cases', option = "C") +
  coord_equal() +
  theme_bw() +
  geom_text(data = dff, aes(long, lat, label = region, group = group), size = 2)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading