5

Following is USA states example data from state.x77 dataset in r:

mydf = structure(list(usa_state = structure(1:5, .Label = c("Alabama", 
"Alaska", "Arizona", "Arkansas", "California"), class = "factor"), 
    Life.Exp = c(69.05, 69.31, 70.55, 70.66, 71.71), HS.Grad = c(41.3, 
    66.7, 58.1, 39.9, 62.6)), .Names = c("usa_state", "Life.Exp", 
"HS.Grad"), class = "data.frame", row.names = c(NA, -5L))

> mydf
   usa_state Life.Exp HS.Grad
1    Alabama    69.05    41.3
2     Alaska    69.31    66.7
3    Arizona    70.55    58.1
4   Arkansas    70.66    39.9
5 California    71.71    62.6
> 

I want to plot it on USA states map. I can plot the map using following code:

all_states <- map_data("state")
ggplot() + geom_polygon( data=all_states, aes(x=long, y=lat, group = group),colour="gray", fill="white" )

enter image description here

But I am not able to plot barcharts over the map. Thanks for your help.

rnso
  • 23,686
  • 25
  • 112
  • 234
  • 1
    You need to learn that there are three distinct 2d plotting paradigms in R. – IRTFM Jun 26 '14 at 05:34
  • Some further details regarding this from an experienced person like you will be much appreciated. – rnso Jun 26 '14 at 06:56
  • @mso - have you had a chance to see if my answer below will be of any help? – micstr Sep 28 '15 at 12:05
  • Thanks for your answer. I had already upvoted it. However, as you can see, the boundaries of USA or its states are not seen in your answer output map. People would prefer colors to be filled in whole map with state boundaries as I have posted in my question above. – rnso Sep 29 '15 at 12:12
  • Possible duplicate of [Plots on a map using ggplot2](http://stackoverflow.com/questions/16028659/plots-on-a-map-using-ggplot2) – Andre Silva Dec 13 '16 at 11:50

1 Answers1

1

I drew on two great sources to answer this:

SOLUTION

mydf <- structure(list(usa_state = 
                         structure(1:5,
                                   .Label = c("Alabama", "Alaska", "Arizona", "Arkansas", "California"), class = "factor"), 
                      Life.Exp = c(69.05, 69.31, 70.55, 70.66, 71.71), 
                      HS.Grad = c(41.3, 66.7, 58.1, 39.9, 62.6)),
                 .Names = c("usa_state", "Life.Exp", "HS.Grad"), 
                 class = "data.frame", row.names = c(NA, -5L))

library(ggplot2)
library(maps)
library(RColorBrewer) # lots of color palettes for these kind of charts

library(data.table) # for sorting by key
library(mapproj) #coord_maps() needed this

all_states <- map_data("state")

# You need to merge dataset with maps one with long and lat.
# But you need same key so lets change state to region used in maps all_states
# Note I lowercased it to get the match

mydf$region <- tolower(mydf$usa_state)
totaldf <- merge(all_states, mydf, by = "region")

# switched to data.table to fix the cut up map issue
# getting sort by region then order 
totaldt <- as.data.table(totaldf)
setkey(totaldt, region, order)

ggplot(data = totaldt, 
       aes(x = long, y = lat, group = group, fill = HS.Grad)) +
  geom_polygon() + coord_map() +
  scale_fill_gradientn("", colours=brewer.pal(9, "GnBu"))

DONT FORGET TO SORT ME

If your data is not sorted correctly by region and then by order, then you will get patchy maps like this.

sliced up map

I use data.table package and key the data. Also data.table is much faster if you need to merge lots of data. You use format X[Y] for this. See data.table cheatsheet if you are new to this package.

FINAL MAP

This is for the HS.Grid in your example. Get your other chart by changing the fill = myvariable

map example for HS.grid

Note not all the states are shown, because the test data is limited to these states. In a fuller example you will see more states.

Also you will see Alaska is missing. Its not in the maps - see this answer from @jazzurro for practical tests on state names with setdiff.

Community
  • 1
  • 1
micstr
  • 5,080
  • 8
  • 48
  • 76