15

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

4
  • 4
    color = NA works just fine for me. Commented Sep 16, 2014 at 18:25
  • It removes the purple but it doesn't remove the space between the polygons. Commented Sep 16, 2014 at 18:27
  • It does for me, perhaps you can post your code and plot with color = NA. Also, maybe make sure you're using an up-to-date version of ggplot2. Commented Sep 16, 2014 at 18:29
  • 1
    color=NA works for me, as does lwd=0. In fact, setting lwd=0 works even if the colour is purple. Is this really a grey outline or is it the background showing through because of sub-pixel accuracy problems in the polygon drawing? What happens if you do this on a red background? Commented Jan 27, 2015 at 14:34

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

An additional note though. Even on Windows, exporting to a PDF leaves borders (or gaps where they would otherwise be) visible. Exporting to PNG matches on screen view, no borders or gaps showing. Even tried adding size=0 but made no difference
6

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

5 Comments

Great, that looks excellent. It's important to note that it works for you given the addition of other code that I did not have.
Strangely it's also not working on my computer. I have version 1.0.0 with fresh updates. I'm on a Mac; is that a potential complication?
Maybe? Are you using RStudio? Try saving as a PDF and see what that yields.
Same thing as a pdf. Yes RStudio. I also notice we have slightly different coloring... it's subtle but might indicate a real difference in platform.
I'm using RStudio on Windows. You could try using cairoDevice? I'm not really sure. Probably the best would be to edit these details back into your question, which will bump it to the top of the active questions list too.
4

Another option is to set both fill and color equal to group, which worked on the macOS I tried it on:

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 = group))

Output:

enter image description here

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.