4

I have managed to create a faceted map from my data following the approach proposed here http://spatial.ly/2013/12/introduction-spatial-data-ggplot2/ with the following code:

ggplot(data = plot.data, aes(x = long, y = lat, fill = value, group = group)) + 
  geom_polygon() + coord_equal() + 
  facet_wrap(~variable,labeller = as_labeller(names), ncol=2)

Where

plot.data <- merge(bound.f, mdata.melt, by.x = "id", by.y = "uid")

and bound.f is the fortified spatial polyshape (with sub-national boundaries) and mdata.melt is the data used in map, in the the long format.

And I get the following output:

output map :In case you don't see, it is the African continent

So far so good. Now I would like to add (overlay) the boundaries of the African countries (which I have a shapefile, same projection than bound) to both maps.

In other words, I want to add the shape, which boundaries should be black and the shape with no fill.

Does anyone have a hint on how I could code this or even has a smarter approach to get beautiful maps into my publication?

2

So i finally made it ! with the following code

   map<- ggplot(data = plot.data, aes(x = long, y = lat, fill = value, group = group)) 
   + geom_polygon() + coord_equal() +
   facet_wrap(~variable,labeller = as_labeller(names))

then

map+geom_path(data=africa.f, aes(x=long, y=lat, group=group), lwd = 0.01,inherit.aes = F)  

The trick was the inherit.aes = F which avoid calling on the previous layer

  • I have the same problem, but even after your solution, the overlayed layer is displayed on top of the previous map, instead of being below it. Any suggestion? – ccamara Sep 22 '17 at 10:51
1

One way would be to get a list of the state names and add them like this

library(XML)
library(ggplot2)
doc <- htmlParse(readLines("https://www.countries-ofthe-world.com/countries-of-africa.html"))
countries <- tolower(xpathSApply(doc, "/html/body/div/div/div/div/div/ul/li[not(@class)]", xmlValue))
ggplot() + borders(regions = countries)

Some names are still missing; you can add them to countries in the veins of countries <- c(countries, "another state", "and another one"). The country names must match the ones in map(plot=F)$names in order to be recognized.

  • Thanks a lot for your support! as i had an existing shapefile for the boundaries i went a different way! – Catherine Pfeifer Apr 7 '16 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.