2
votes

I am trying to produce a choropleth map using geom_map in ggplot2. I want to outline the various regions in black or some other color to distinguish between areas that are similar in color on the gradient. Using the following code I've tried to set the color to outline the areas. The code produces the map, but no borders. When I move the colour command into the aesthetic it produces the expected 'pink' borders with a legend. Any ideas why setting the color won't produce borders, but mapping will? I saw a similar discussion on the ggplot2 Google Group.

ggplot(subset(df, as.character(variable) == "value"), aes(map_id = id)) +
  geom_map(aes(fill = pct), colour = "black", map = ggmap) +
  expand_limits(x = ggmap$long, y = ggmap$lat) +
  scale_fill_gradient(low = "antiquewhite", high = "darkred") +
  opts(title = "Title", panel.background = theme_rect(fill = "grey90"))

Thanks

1

1 Answers

4
votes

You need to add the polygon borders using geom_polygon. In the code below you need to fill in the XXXX with a data set (data) and lat and long (x and y) values for each polygon. I usually get my data from the maps package, not sure if this is what you did.

ggplot(subset(df, as.character(variable) == "value"), aes(map_id = id)) +
  geom_map(aes(fill = pct), colour = "black", map = ggmap) +
  geom_polygon(data=XXXX, aes(x=XXXX, y=XXXX), colour='black', fill=NA) +
  expand_limits(x = ggmap$long, y = ggmap$lat) +
  scale_fill_gradient(low = "antiquewhite", high = "darkred") +
  opts(title = "Title", panel.background = theme_rect(fill = "grey90"))