I am trying to generate a world map with ggplot, I want Peru and Thailand to be in yellow. Madagascar, Mexico and Brazil in green, and Kenya in red

Viewed 88

I tried this:

library(ggplot2)
library(dplyr)

country.type = map_data("world")

country.type <- mutate(country.type, fill = ifelse(region %in% c("Madagascar", "Mexico","Brazil"), "springgreen", "grey80"))

ggplot(country.type, aes(long, lat, fill = fill, group=group)) + 
geom_polygon(colour="gray80")+ 
scale_fill_identity()+theme_classic() +
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.background = element_rect(colour= "black", size= 1))

But, I can't paint the other countries

enter image description here

1 Answers

You are almost there. You need to specify the color for other countries as well:

country.type <- country.type %>%
    mutate(fill = case_when(
        region %in% c("Madagascar", "Mexico","Brazil") ~ 'springgreen',
        region %in% c('Peru', 'Thailand') ~ 'yellow',
        region %in% c('Kenya') ~ 'red',
        TRUE ~ 'grey80'
    ))

Then with your plot code above: enter image description here

Related