Adding colour to specific countries in a world map using ggplot

Viewed 1503

I am trying to generate a plot of a world map wusing ggplot in which USA, Canada, and Brazil are in red.

This is what I have tried

thismap = map_data("world")

ggplot(thismap, aes(long, lat, group=group))+geom_polygon(fill="white", colour="gray")+
  ggtitle("Map of World")

But, I could not obtain those countries in red

1 Answers

Try this. I add a column to the df where I set the colors. Inside ggplot map this column on the fill aestehtic(!!). To get the right colors use scale_fill_identity.

library(ggplot2)
library(dplyr)

thismap = map_data("world")

# Set colors
thismap <- mutate(thismap, fill = ifelse(region %in% c("Brazil", "Canada", "USA"), "red", "white"))

# Use scale_fiil_identity to set correct colors
ggplot(thismap, aes(long, lat, fill = fill, group=group)) + 
  geom_polygon(colour="gray") + ggtitle("Map of World") + 
  scale_fill_identity()

Created on 2020-04-05 by the reprex package (v0.3.0)

Related