Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Borders and colors on world map ggplot2

Tags:

r

ggplot2

I am trying to produce an elegant map of countries in which an organization has interest in working. I have produced the map below, but want to make things more elegant by removing the country borders, everywhere EXCEPT where two countries of interest share a border - e.g. between South Africa and Zimbabwe. I have been through a number of tutorials and can't find anything on this.

Finally i would like to add a legend for the city data.

Here is the code:

world <- map_data("world") 
countries <- read_excel("country_table.xlsx", sheet = 3) #table of coutries with interest

world3 <- merge(world, countries, all.x = TRUE)
world4 <- arrange(world4, order)

city <- read_excel("country_table.xlsx", sheet = 4) #city data
city$long <- as.numeric(city$long)
city$lat <- as.numeric(city$lat)

ggplot(world4, aes(x = long, y = lat, group = group, fill = interest)) +
  geom_polygon(col = "white") +
  #scale_fill_manual(breaks = c("interest", "past", "current"), values = c("#4dc11d","#26660b","#9def7a")) +
  theme_map() +
  coord_fixed(xlim = c(-130, 160), ylim = c(-50, 75), ratio = 1.3) +
  geom_point(data = city, aes(x= long, y = lat), shape = 21, inherit.aes = F, size = 2, col = "black", fill = "yellow")

Produces:

enter image description here

Further I would like to use a scale of green to represent the countries, with the legend in the order: "interest", "past", "current"; going from light green to dark green. At the moment when i uncheck the commented line "scale_fill_manual" i lose all the NA data producing the image below. I have tried adding this through multiple means, but can't get it to work. To make clear what i mean, this is what the code produces when i uncheck that comment:

enter image description here

Any help on either issue would be appreciated.

like image 329
MorrisseyJ Avatar asked Jul 15 '18 17:07

MorrisseyJ


1 Answers

Form your plot in layers. This should also take care of the scale_fill_manual problem, because you won't have NA values in your data frame.

In addition to what you've done:

# subset for all countries that are filled by a color
library(dplyr)
world5 <- world4 %>% dplyr::filter(!is.na(interest))

ggplot() +
  # first layer: all countries, no fill, no white outlines
  geom_polygon(data = world4, 
               aes(x = long, y = lat, group = group)) +
  # second layer: only countries with a fill
  geom_polygon(data = world5, 
               aes(x = long, y = lat, group = group, fill = interest),
               color = "white") +
  # apply the color scale
  scale_fill_manual(breaks = c("interest", "past", "current"), 
                    values = c("#4dc11d","#26660b","#9def7a"))                 
like image 177
Rich Pauloo Avatar answered Oct 10 '22 18:10

Rich Pauloo