Google Groups

Re: help on geom_map


Allan Just Apr 5, 2012 9:55 AM
Posted in group: ggplot2
Hi S.N.V.,
I made fake data to reproduce your plot and answer your three queries. The first two are easy and have to do with setting vs mapping colour to a constant and the na.value option in scale_fill_continuous. To put labels on states you need a place to put them. Just averaging the lat & lon of the points that make up their path weights toward the jagged side of states with irregular geometries like Arkansas so I adapted code from http://stackoverflow.com/questions/8751497/latitude-longitude-coordinates-to-state-code-in-r in order to compute a centroid (well, it looks right to me).
-Allan

# self-contained example of state choropleth map with missingness and labels
library(ggplot2)
library(scales)
library(maps)

states = map_data('state')
fake <- data.frame(us.states = unique(states$region),
    area_percent = runif(runif(49, 0, 1)))
fake$area_percent[-sample(1:49, 10)] <- NA

# reproduce your map
ggplot(fake, aes(map_id = us.states)) +
    geom_map(aes(fill = area_percent, colour = 'grey90'),
        map = states) +
    expand_limits(x = states$long, y = states$lat)

# a) set instead of map colour
#    don't put it in aes() if it takes a constant value
# b) change NA to light grey
P1 <- ggplot(fake, aes(map_id = us.states)) +
    geom_map(aes(fill = area_percent), colour = 'grey90', map = states) +
    scale_fill_continuous(na.value = "grey75") +
    expand_limits(x = states$long, y = states$lat)
P1

# c) add text labels to all states - compute centroids first
library(maptools)
states2 <- map('state', fill=TRUE, col="transparent", plot=FALSE)
IDs <- sapply(strsplit(states2$names, ":"), function(x) x[1])
states_sp <- map2SpatialPolygons(states2, IDs=IDs,
    proj4string=CRS("+proj=longlat +datum=wgs84"))
state.label <- data.frame(us.states = unique(IDs), coordinates(states_sp))
P1 + geom_text(aes(X1, X2, label = us.states), data = state.label)

# just label the states with a value for your fill variable
P1 + geom_text(aes(X1, X2, label = us.states),
    data = state.label[state.label$us.states %in%
        fake$us.states[!is.na(fake$area_percent)], ])



On Thursday, 5 April 2012 06:00:33 UTC-4, SNV Krishna wrote:

Hi All,

 

I am trying to create a choropleth map of US states based on variable named “area_percent” from data frame “soybn.area2”.

I got the basic version (plot enclosed) but would like to fine tune a bit and need members help in this regard. My data frame looks like this

 

> rbind(head(soybn.area2),tail(soybn.area2))

        us.states X2010 X2011 X2012 area_percent

1         alabama   350   300   310         0.42

2         arizona    NA    NA    NA           NA

3        arkansas  3190  3330  3300         4.47

4      california    NA    NA    NA           NA

5        colorado    NA    NA    NA           NA

6     connecticut    NA    NA    NA           NA

58      virginia    560   560   590         0.80

59  south dakota   4200  4100  4300         5.82

60     tennessee   1450  1290  1240         1.68

61         texas    205   165   125         0.17

62 west virginia     20    20    19         0.03

63     wisconsin   1640  1610  1680         2.27

 

My code to create the basic plot is given below

 

> library(ggplot2)

> library(scales)

> library(maps)

> states = map_data('state')

 

> ggplot(soybn.area2, aes(map_id = us.states)) + geom_map(aes(fill = area_percent, colour = 'grey90'), map = states)+

      expand_limits(x = states$long, y = states$lat)

 

Now I would like to fine tune the graph with following

 

a) change state border colour to “grey90” vs. red as it was appearing on plot.

b) change the fill colour of states whose “area_percent” value is “NA” to some light grey (vs. existing dark grey)

c) add text label to each state on chart with those of corresponding “us.states”

 

Many thanks for the help and inputs.

 

Best regards,

 

S.N.V. Krishna