29

I am trying to create a thematic map showing all 50 US states, but I am having trouble relocating Alaska and Hawaii in a reliable way. I have a couple of ideas but none of them work well. I will demonstrate them now.

First we need to import the data; using the data in the maps package is not enough because it does not include Hawaii and Alaska.

setwd(tempdir())
download.file("https://dl.dropbox.com/s/wl0z5rpygtowqbf/states_21basic.zip?dl=1", 
              "usmapdata.zip", 
              method = "curl")
# This is a mirror of http://www.arcgis.com/home/item.html?
# id=f7f805eb65eb4ab787a0a3e1116ca7e5
unzip("usmapdata.zip")

require(rgdal)
all_states <- readOGR("states_21basic/", "states")

require(ggplot2); require(maptools); require(rgeos); require(mapproj);
all_states <- fortify(all_states, region = "STATE_NAME")

Now we define some plot aesthetics:

p <- ggplot() + geom_polygon( 
  aes(x=long, y=lat, group = group, fill = as.numeric(as.factor(id))), 
  colour="white", size = 0.25
) + coord_map(projection="azequalarea") + 
scale_fill_gradient(limits = c(1,50))

Now we remove all background etc so they don't clash when we overlap the non-contiguous states:

p <-   p + theme(axis.line=element_blank(),
            axis.text.x=element_blank(),
            axis.text.y=element_blank(),
            axis.ticks=element_blank(),
            axis.title.x=element_blank(),
            axis.title.y=element_blank(),
            panel.background=element_blank(),
            panel.border=element_blank(),
            panel.grid.major=element_blank(),
            panel.grid.minor=element_blank(),
            plot.background=element_blank())

Using viewports

The first idea I had was to use viewports:

AK <- p %+% subset(all_states, id == "Alaska") + theme(legend.position = "none")
HI <- p %+% subset(all_states, id == "Hawaii") + theme(legend.position = "none")
contiguous <- p %+% subset(all_states, id != "Alaska" & id != "Hawaii")

grid.newpage()
vp <- viewport(width = 1, height = 1)
print(contiguous, vp = vp)
subvp1 <- viewport(width = 0.25, height = 0.25, x = 0.18, y = 0.33)
print(AK, vp = subvp1)
subvp2 <- viewport(width = 0.12, height = 0.12, x = 0.32, y = 0.27)
print(HI, vp = subvp2)

enter image description here

This looks nice but it is not satisfactory because it is very sensitive to slight changes in the figure, for example resizing or changes in the size and shape of the legend.

Manually moving Alaska and Hawaii

all_states_AKHImoved <- within(all_states, {
  lat[id == "Alaska"] <- lat[id == "Alaska"] - 45
  long[id == "Alaska"] <- long[id == "Alaska"] + 40
  lat[id == "Hawaii"] <- lat[id == "Hawaii"] + 0
  long[id == "Hawaii"] <- long[id == "Hawaii"] + 70
})
p %+% all_states_AKHImoved

enter image description here

This is not satisfactory because Alaska is usually not to scale on most US maps so it looks very big. Also relocating Alaska and Hawaii changes the distortion introduced by the map projection.

Question

Does anyone have any better approaches?

2
  • missing require(mapproj), and ggplot(data =all_states)
    – agstudy
    Dec 7, 2012 at 6:31
  • Thank you, I have added require(mapproj). I do not think ggplot(data = all_states) is required because the data is added later using the %+% operator. This permits the construction of maps showing just Alaska, Hawaii or the 48 contiguous states.
    – orizon
    Dec 7, 2012 at 6:37

3 Answers 3

15

I've published the fiftystater R package on GitHub (devtools::install_github("wmurphyrd/fiftystater")) to provide a simple solution. It is based on the steps from Spacedman's answer (would link but insufficient rep) and is published as a ggplot2::geom_map ready shape data frame named fifty_states to remove the need to install dependencies, track down a source shape file, or tweak elide values.

library(ggplot2)
library(mapproj)
library(fiftystater)

crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)

p <- ggplot(crimes, aes(map_id = state)) + 
  geom_map(aes(fill = Assault), map = fifty_states) + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map()
p

fifty states map fifty states map

Plot noise can be cleaned up in the usual way, and there is also the fifty_states_inset_boxes function in the package to add inset boxes:

p + scale_x_continuous(breaks = NULL) + 
  scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") +
  theme(panel.background = element_blank()) +
  fifty_states_inset_boxes()

fifty states cleaner with inset boxes fifty states with inset boxes

2
  • I want to make the same kind of map, but to Mexico. Can I use the data of all cities? Where did you find the data for long and lat to US?
    – Arduin
    Apr 10, 2018 at 22:46
  • 1
    Needs devtools::install_github("wmurphyrd/fiftystater")
    – dca
    Feb 29, 2020 at 18:23
14

Here's how to do it by projecting and transforming. You will need:

require(maptools)
require(rgdal)

fixup <- function(usa,alaskaFix,hawaiiFix){

  alaska=usa[usa$STATE_NAME=="Alaska",]
  alaska = fix1(alaska,alaskaFix)
  proj4string(alaska) <- proj4string(usa)

  hawaii = usa[usa$STATE_NAME=="Hawaii",]
  hawaii = fix1(hawaii,hawaiiFix)
  proj4string(hawaii) <- proj4string(usa)

  usa = usa[! usa$STATE_NAME %in% c("Alaska","Hawaii"),]
  usa = rbind(usa,alaska,hawaii)

  return(usa)

}

fix1 <- function(object,params){
  r=params[1];scale=params[2];shift=params[3:4]
  object = elide(object,rotate=r)
  size = max(apply(bbox(object),1,diff))/scale
  object = elide(object,scale=size)
  object = elide(object,shift=shift)
  object
}

Then read in your shapefile. Use rgdal:

us = readOGR(dsn = "states_21basic",layer="states")

Now transform to equal-area, and run the fixup function:

usAEA = spTransform(us,CRS("+init=epsg:2163"))
usfix = fixup(usAEA,c(-35,1.5,-2800000,-2600000),c(-35,1,6800000,-1600000))
plot(usfix)

The parameters are rotations, scaling, x and y shift for Alaska and Hawaii respectively, and were obtained by trial and error. Tweak them carefully. Even changing Hawaii's scale parameter to 0.99999 sent it off the planet because of the large numbers involved.

If you want to turn this back to lat-long:

usfixLL = spTransform(usfix,CRS("+init=epsg:4326"))
plot(usfixLL)

But I'm not sure if you need to use the transformations in ggplot since we've done that with spTransform.

You can now jump through the ggplot2 fortify business. I'm not sure if it matters for you but note that the order of the states is different in the usfix version - Alaska and Hawaii are now the last two states.

1
  • I also note you said 50 states, but this data set has 50 states and Washington DC :)
    – Spacedman
    Dec 7, 2012 at 17:23
3

Once you start shifting things around like this you may as well just represent Alaska and Hawaii as square boxes somewhere in the Gulf of Mexico. It would have the added advantage of making it possible to tell what colour Hawaii was.

You could probably then just go all the way and use a distorted system where each state has equal area, then you could see Rhode Island.

USA Cartogram examples on google images shows the kind of thing. Don't know how many of them have shapefiles or data with them though.

Are you really interested in the relative sizes of states, or do you want a representation that lets people see what the value is for a state?

3
  • 1
    Those are sensible suggestions but I don't think what I want to do is particularly unusual. Many thematic maps are produced with AK and HI relocated preserving their shape to produce a more condensed map. I want a reliable way to generate such maps in ggplot2 without the need to fine tune parameters every time. I am interested in cartograms but I think that would be much harder.
    – orizon
    Dec 7, 2012 at 9:17
  • Just because its common doesn't make it good :) One thing you might try to do is to find a shapefile where Alaska and Hawaii are in the right place. Moving them yourself is a bit tricky, as you have found. What you need to do to keep the basic shape is to transform it in the projected coordinate system rather than lat-long, and then back-transform to lat-long. You can project map data with spTransform from package:rgdal (if I get time today I'll have a play, you have given us a nice example so you deserve it!)
    – Spacedman
    Dec 7, 2012 at 10:26
  • Honestly, many of the Cartogram examples you link to send cold shivers down my statistical spine...
    – Joris Meys
    Dec 7, 2012 at 17:35

Your Answer

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

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