Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove border lines in ggplot map/choropleth

Tags:

r

maps

ggplot2

I would like to remove the lines between regions of a choropleth generated in ggplot. My question is motivated by a very large map with very very small regions (census block groups) that are so numerous that it's impossible to see the color filling the shape given the density of the borders. I'm using updated RStudio on a Mac with ggplot2 version 1.0.0; the same problem does not seem to occur on Windows.

Here are examples (using counties) that have different colors for each county so that the borders are not necessary. The first uses purple borders for emphasis. The second has color = NA which was my unsuccessful attempt to eliminate all borders.

library("ggplot2")
library("maps")
tn = map_data("county", region = "tennessee")
ggplot(tn, aes(x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = group), color = "purple")

enter image description here

ggplot(tn, aes(x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = group), color = NA)

enter image description here

like image 356
Nancy Avatar asked Sep 16 '14 18:09

Nancy


2 Answers

Setting color = NA works for me:

ggplot(tn, aes(x = long, y = lat, group = group)) + 
    geom_polygon(aes(fill = group), color = NA) +
    coord_map()

produces this plot with no spaces between polygons.

tn-map-no-borders

I'm using ggplot2 version 1.0.0.

I added coord_map to give it the right aspect ratio. On my machine, that doesn't affect the borders, I'm not sure why borders are visible in your second post. Here's mine:

ggplot(tn, aes(x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = group), color = NA)

enter image description here

like image 79
Gregor Thomas Avatar answered Nov 01 '22 05:11

Gregor Thomas


I can confirm it's specific to the Mac. Was just trying to do the same and 'colors=NA' has no visible effect in R Studio on a Mac, the borders still show. Just loaded the project on Windows and the borders are gone.

For reference, my set-up: Mac is running R Studio 0.98.1074 on Mac OS X 10_10_1 (Yosemite). Windows is running R Studio 0.98.1073 on Windows 7.

like image 7
Joining Dots Avatar answered Nov 01 '22 07:11

Joining Dots