Home > Back-end >  How to fill countries with their respective flags with geom_map?
How to fill countries with their respective flags with geom_map?

Time:07-12

I'm working with package ggplot2 and using the function geom_map to fill the countries with different colors.

I was wondering if it is possible to fill with flags instead of colors?

Desired result:

enter image description here

I'm doing:

ALL_EU <-c("Russia", "Germany", "UK", "France", "Italy", "Spain", "Ukraine", "Poland", "Romania", "Netherlands", "Belgium", "Czech Republic", "Greece", "Portugal", "Sweden", "Hungary", "Belarus", "Austria", "Serbia", "Switzerland", "Bulgaria", "Denmark", "Finland","Slovakia", "Norway", "Ireland", "Croatia", "Moldova", "Bosnia and Herzegovina","Albania", "Lithuania", "North Macedonia", "Slovenia", "Latvia", "Estonia","Montenegro", "Luxembourg"  , "Malta", "Iceland", "Andorra", "Monaco", "Liechtenstein","San Marino", "Vatican", "Kosovo")

#Filter some regions out just for plot
world_map <- map_data("world",region = ALL_EU[-which(ALL_EU%in%c("Russia","Iceland"))])
world_map <- world_map%>%filter(subregion!="Svalbard"&subregion!="Jan Mayen"|is.na(subregion))

and then plot it, note that in this case I use fill = "Flags" so I can then change the color and legend with scale_fill_manual, also I make all countries blue just for a reproducible example.

ggplot()  
    theme_void() 
    geom_map(dat = world_map,map = world_map, 
             aes(map_id = region,fill = "Flags"),color = "gray",size = 0.25) 
    scale_fill_manual(name = "Countries", values = c("Flags" = "blue")) 
    expand_limits(x = world_map$long, y = world_map$lat)

enter image description here

Also, and even if its possible, fill several countries with, for example, the flag of the European Union. But this is probably for another question. Thank you!

CodePudding user response:

Here is an option using the excellent enter image description here

  • Related