11

How do you make a 50 state map in R?

It seems like all the example maps people have created are just of the lower 48

Jaap
  • 81,064
  • 34
  • 182
  • 193
Marc
  • 119
  • 1
  • 4
  • These maps are called "choropleths." A Google search for "choropleth 50 state" found me the package `choroplethr` that can generate maps with all 50 states. – shadowtalker Aug 27 '14 at 15:00

5 Answers5

16

There are lots of ways that you can do this. Personally, I find Google to have the most attractive maps. I recommend ggmap, googleVis, and/or RgoogleMaps.

For example:

require(googleVis)

G4 <- gvisGeoChart(CityPopularity, locationvar='City', colorvar='Popularity',
                   options=list(region='US', height=350, 
                                displayMode='markers',
                                colorAxis="{values:[200,400,600,800],
                                 colors:[\'red', \'pink\', \'orange',\'green']}")
) 
plot(G4)

Produces this:

enter image description here

Another approach that will give you a more attractive result than maps is to follow the approach of this tutorial which shows how to import custom maps from Inkscape (or, equivalently, Adobe Illustrator) into R for plotting.

You'll end up with something like this:

R and Inkscape

Here's a way to it with choroplethr and ggplot2:

library(choroplethr)
library(ggplot2)
library(devtools)
install_github('arilamstein/choroplethrZip@v1.3.0')
library(choroplethrZip)

data(df_zip_demographics)
df_zip_demographics$value = df_zip_demographics$percent_asian

zip_map = ZipChoropleth$new(df_zip_demographics)
zip_map$ggplot_polygon = geom_polygon(aes(fill = value),
                                      color = NA)
zip_map$set_zoom_zip(state_zoom = NULL,
                     county_zoom = NULL,
                     msa_zoom = NULL,
                     zip_zoom = NULL)
zip_map$title = "50 State Map for StackOverflow"
zip_map$legend = "Asians"
zip_map$set_num_colors(4)
choro = zip_map$render()
choro

data(df_pop_state)
outline = StateChoropleth$new(df_pop_state)
outline = outline$render_state_outline(tolower(state.name))

choro_with_outline = choro + outline
choro_with_outline

which gives you:

ggplot2

Hack-R
  • 22,422
  • 14
  • 75
  • 131
6

This R-bloggers link might be useful for you.

To give you a look, you can get started on a 50-state map with

library(maps)
map("world", c("USA", "hawaii"), xlim = c(-180, -65), ylim = c(19, 72))

enter image description here

Believe it or not, Hawaii is on there. It's just really small because of the margins.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
6

Resurrecting an old thread because it still doesn't have an accepted answer.

Check out @hrbrmstr's albersusa package:

devtools::install_github("hrbrmstr/albersusa")
library(albersusa)
plot(usa_composite(proj="laea"))

which produces

enter image description here

and can do much more

us <- usa_composite()
us_map <- fortify(us, region="name")

gg <- ggplot()
gg <- gg + geom_map(data=us_map, map=us_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#2b2b2b", size=0.1, fill=NA)
gg <- gg + theme_map()

gg + 
  geom_map(data=us@data, map=us_map,
           aes(fill=pop_2014, map_id=name),
           color="white", size=0.1) +
  coord_proj(us_laea_proj) +
  scale_fill_viridis(name="2014 Populaton Estimates", labels=comma) +
  theme(legend.position="right")

enter image description here

Jonathan Carroll
  • 3,897
  • 14
  • 34
2

You might want to consider using the state.vbm map in the maptools package, this includes all 50 states and makes the smaller states more visible (works fine for coloring, or adding plots to each state, but distances between sites will not be exact).

Another option is to draw the contiguous 48 states, then in open areas add Alaska and Hawaii yourself. One option for doing this is to use the subplot function from the TeachingDemos package.

Here is some example code using a couple of the shapefiles provided by the maptools package:

library(maptools)
library(TeachingDemos)

data(state.vbm)
plot(state.vbm)

yy <- readShapePoly(system.file("shapes/co37_d90.shp", package="maptools")[1])
zz <- readShapePoly(system.file("shapes/co51_d90.shp", package="maptools")[1])
xx <- readShapePoly(system.file("shapes/co45_d90.shp", package="maptools")[1])
plot(yy)
par('usr')

subplot( plot(zz), c(-84,-81), c(31,33) )
subplot( plot(xx), c(-81, -78), c(31,33) )

You would just need to find the appropriate shape files for the states.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
2

Using choroplethr you can create a simple and quick state map by doing the following:

#install.packages("choroplethr")
#install.packages("choroplethrMaps")

library(choroplethr)
library(choroplethrMaps)
data(df_pop_state)
StateChoropleth$new(df_pop_state)$render()

I like this method because it's fast and easy. If you don't want the state labels, removing them requires a little bit more:

c = StateChoropleth$new(df_pop_state)
c$title = "2012 State Population Estimates"
c$legend = "Population"
c$set_num_colors(7)
c$set_zoom(NULL)
c$show_labels = FALSE
without_abbr = c$render()

without_abbr

Here's a comparison of the two methods: enter image description here

Sources 1 and 2

cacti5
  • 2,006
  • 2
  • 25
  • 33