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

ggplot add country names in legend below geom_map

I would like to add a legend for the country names below my map.

I have this dataframe of frequency of event occurrences on different regions:

trend_country_freq  <- structure(list(country = c("US", "CN", "KR", "IN", "AU", "GB", 
"JP"), n = c(25L, 20L, 12L, 5L, 2L, 1L, 1L), country_name = c("USA", 
"China", "South Korea", "India", "Australia", "UK", "Japan")), row.names = c(1L, 
2L, 3L, 4L, 5L, 7L, 8L), class = "data.frame")

Now I use the maps and ggplot2 packages to create a world map showing the frequency of event occurences:

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

library(maps)
library(ggplot2)

world_map <- map_data("world")
world_map <- subset(world_map, region != "Antarctica")

ggplot(trend_country_freq) +
  geom_map(
    dat = world_map, map = world_map, aes(map_id = region),
    fill = "white", color = "#7f7f7f", size = 0.25
  ) +
  geom_map(map = world_map, aes(map_id = country_name, fill = n), size = 0.25) +
  scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Total Cases") +
  expand_limits(x = world_map$long, y = world_map$lat) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank()) + 
  theme(axis.title = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank())

The result looks like this:

enter image description here

But I actually want something like this:

enter image description here

Do you have ideas how to generate such a map? Thank you very much!

>Solution :

Here’s a hack, not sure how awesome it is:

transform(trend_country_freq,                                            # NEW
  txt = sprintf("%s (%i)", country_name, n),                             #
  vj = -1.2 * seq(nrow(trend_country_freq))                              #
) |>                                                                     #
  ggplot() +                                                             # CHANGED
  geom_map(
    dat = world_map, map = world_map, aes(map_id = region),
    fill = "white", color = "#7f7f7f", size = 0.25
  ) +
  geom_map(map = world_map, aes(map_id = country_name, fill = n), size = 0.25) +
  scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Total Cases") +
  expand_limits(x = world_map$long, y = world_map$lat) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank()) + 
  theme(axis.title = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank()) +
  geom_text(aes(label = txt, vjust = vj), x = -Inf, y = -Inf, hjust = 0) # NEW

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