7

I'm trying to plot a US map where each state is shaded by the count that it has. I've gotten the shading to work just fine. The problem I'm running into, however, is that the polygons look very jagged (I'm assuming something happened when I tried to merge the map_data('state') with my data frame of counts per state). My data frame before merging has 49 rows (Nevada was missing data in my set), and after merging has many more rows (expected for the long/lat items for the states) but the data appears to be copied correctly for each lat/long pair, so I'm unsure why the poly's are so jagged.

Code:

ggplot() +
  geom_polygon(data=try1, aes(x=long, y=lat, group = group, fill= COUNT)) +
  scale_fill_continuous(low='thistle2', high='darkred', guide='colorbar') +
  theme_bw() + labs(fill="State Map Try Title1", title='Title2', x='', y='') +
  scale_y_continuous(breaks=c()) +
  scale_x_continuous(breaks=c()) +
  theme(panel.border = element_blank())

Any help would be greatly appreciated (and obviously, if there is a better way to do it, I'm open to suggestions!).

enter image description here

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
Keyboard Frenzy
  • 81
  • 1
  • 1
  • 6
  • 1
    Example Image: ![Rplot](http://i.imgur.com/2S3PHK0.png) I apparently don't know how to embed images. >.> – Keyboard Frenzy Apr 13 '15 at 21:05
  • 1
    Can you provide: the shapefile you've used; your data of counts; and the code you used to join so we can look at which stage is going wrong? Thanks. – Phil Apr 13 '15 at 22:14
  • Hi Phil, I did the following: states_map <- map_data('state') #this contains the long/lat for the states shapes merge(states_map, data, by='region') I don't have access to the data right now (at a different computer) but I'll try to post the before merge data table I'm using. Thanks! – Keyboard Frenzy Apr 13 '15 at 23:26
  • Your data got ordered incorrectly. If you use `plyr::join` or `dplyr::left_join` instead of `merge`, you should be fine. Alternately, you could add row numbers to your geo data at the start and sort by them at the end. Also check `coord_map` for a decent projection. – Gregor Thomas Apr 14 '15 at 00:10
  • plyr::join worked perfectly. Just changed `merge(*args and stuff)` to `join(*args and stuff)` and not only did it join faster, but it the graphs look flawless. Thank you! – Keyboard Frenzy Apr 14 '15 at 01:49

2 Answers2

14

You don't need to do the merge. You can use geom_map and keep the data separate from the shapes. Here's an example using the built-in USArrests data (reformatted with dplyr):

library(ggplot2)
library(dplyr)

us <- map_data("state")

arr <- USArrests %>% 
  add_rownames("region") %>% 
  mutate(region=tolower(region))

gg <- ggplot()
gg <- gg + geom_map(data=us, map=us,
                    aes(x=long, y=lat, map_id=region),
                    fill="#ffffff", color="#ffffff", size=0.15)
gg <- gg + geom_map(data=arr, map=us,
                    aes(fill=Murder, map_id=region),
                    color="#ffffff", size=0.15)
gg <- gg + scale_fill_continuous(low='thistle2', high='darkred', 
                                 guide='colorbar')
gg <- gg + labs(x=NULL, y=NULL)
gg <- gg + coord_map("albers", lat0 = 39, lat1 = 45) 
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.background = element_blank())
gg <- gg + theme(axis.ticks = element_blank())
gg <- gg + theme(axis.text = element_blank())
gg

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Hey thanks! This works as well. I especially appreciate the coord_map since it makes the map a bit more pretty. – Keyboard Frenzy Apr 14 '15 at 02:47
  • I can't get this to run. First `%>%` is not available by default and hard to google for (it's from `magrittr`). Second I can't get `dplyr` to install on 3.1.1. Can you edit the column shuffling to be more minimal? – Superbest May 26 '16 at 08:20
  • The code runs perfectly in a vanilla R session and `%>%` comes along for the ride with `dplyr`. Which means you didn't run it as-is. Also, I'm sure you are more than capable of putting the code into your own editor and changing line spacing/indenting to suit your personal preferences, @Superbest – hrbrmstr May 26 '16 at 11:12
  • Well it didn't work for me, I use Rstudio. Column shuffling refers to columns of data frame, not the source. Anyway, excuse me for trying to help improve the answer. – Superbest Jun 02 '16 at 18:57
  • You did not execute the code the way it is written. Period. I use RStudio as well. – hrbrmstr Jun 02 '16 at 18:59
  • Code works perfectly if you follow it with a new & clean install. Thanks @hrbrmstr. One additional helper in case people are looking for alternative colour palettes, the following will go from blue to red through white: `scale_fill_gradientn(colours = c("#2b8cbe", "white", "#d7301f"))` – Achekroud Oct 29 '16 at 01:26
  • You should absolutely not do that color palette. 10% of the world can't see it properly. Look at the `viridis` package. I only used the colors the OP had since it was what the OP had. – hrbrmstr Oct 29 '16 at 11:38
0

I encountered a similar problem when using ggplot2. As an alternative to @hrbrmstr 's solution, I found reloading the ggplot2 package (as well as rgdal/`maptools') resolved the issue for me.

Phil
  • 4,344
  • 2
  • 23
  • 33