2

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

0

1 Answer 1

4

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"))
4
  • Thanks Tyler. This is exactly what I was missing. I'm using a shape file that outlines outlines attendance zones for schools in a school district.
    – Mike
    Apr 24, 2012 at 21:15
  • I have a question that is very similar to this one, posted here, in case you have time to look at it i'd be thankful for comments.
    – yellowcap
    May 13, 2012 at 15:22
  • @TylerRinker Could you clarify the how getting your coordinates from the maps package would work if you want to highlight state borders in a different color. I know there is the "borders("state"), but how would you do it here using geom_polygon?
    – CRSouser
    May 17, 2015 at 1:05
  • I'd suggest opening a new question. This one is 3 years old and ggplot2 has changed since then. Provide data in your question and be explicit in what you're after. You can ink back to this question. May 17, 2015 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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