6

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.

MorrisseyJ
  • 1,191
  • 12
  • 19
  • Do you not get an error saying you've provided too few colors? Try setting an `na.value` color in your fill scale – camille Jul 15 '18 at 20:12
  • I also think this might be better handled as separate questions, since there are multiple discrete tasks you're trying to do, some of which most likely have answers already. You've got 1. making a union of / dissolving shapes, in order to get rid of borders between non-flagged countries; 2. making a legend from a dummy aesthetic, to get a city legend (which is going to require posting some data); 3. handling NA values and setting an appropriate fill scale. – camille Jul 15 '18 at 20:17

1 Answers1

3

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"))                 
Rich Pauloo
  • 7,734
  • 4
  • 37
  • 69
  • 1
    Thanks, Rich Pauloo. This worked pretty perfectly. Adding the geom_polygon with all countries but no fill and no color (for the borders), and then adding another polygon on top of that with the selected countries, resolved the border issue and meant that the scale_fill_manual command worked. I will look to resolve the city legend issue in another post, as per camille's suggestion. – MorrisseyJ Jul 16 '18 at 17:34
  • Question on legend is posted [here](https://stackoverflow.com/questions/51369515/secondary-legend-on-chloropleth-for-points-and-polygons-ggplot) – MorrisseyJ Jul 17 '18 at 23:37