help on geom_map

539 views
Skip to first unread message

SNV Krishna

unread,
Apr 5, 2012, 6:00:33 AM4/5/12
to ggp...@googlegroups.com

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

 

soybn_area.PNG

Allan Just

unread,
Apr 5, 2012, 12:55:36 PM4/5/12
to ggp...@googlegroups.com
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)], ])

SNV Krishna

unread,
Apr 11, 2012, 11:43:22 PM4/11/12
to Allan Just, ggp...@googlegroups.com

Hi Allan,

 

I just returned from a holiday. Thanks for your inputs. I redraw chart using your code. It worked for na.value and text labels but setting the colour outside aes() seems did not work.

I guess the colour argument outside aes() in geom_map is being ignored. As in the statement (below) I changed setting the colour = ‘red’ instead of ‘grey90’, but there is no effect on plot.

 

….geom_map(aes(fill = area_percent), colour = 'grey90', map = states) +

Could you please check at your end,

 

Thanks and Regards,

 

S.N.V. Krishna

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Allan Just

unread,
Apr 12, 2012, 10:10:19 AM4/12/12
to ggp...@googlegroups.com, Allan Just
Hi S.N.V.,
when I run the code that I posted, the outlines are no longer red and are set to grey90 (see attachment). Maybe there is a difference in package versions? Here is my sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-mingw32/x64 (64-bit)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
 [1] maptools_0.8-14 lattice_0.20-6  sp_0.9-98       foreign_0.8-49  maps_2.2-5    
 [6] scales_0.2.0    ggplot2_0.9.0   JGR_1.7-9       iplots_1.1-4    JavaGD_0.5-5  
[11] rJava_0.9-3   

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.0      
 [5] MASS_7.3-17        memoise_0.1        munsell_0.3        plyr_1.7.1       
 [9] proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1     stringr_0.6      
[13] tools_2.15.0   

-Allan


On Wednesday, 11 April 2012 23:43:22 UTC-4, SNV Krishna wrote:

Hi Allan,

 

I just returned from a holiday. Thanks for your inputs. I redraw chart using your code. It worked for na.value and text labels but setting the colour outside aes() seems did not work.

I guess the colour argument outside aes() in geom_map is being ignored. As in the statement (below) I changed setting the colour = ‘red’ instead of ‘grey90’, but there is no effect on plot.

 

….geom_map(aes(fill = area_percent), colour = 'grey90', map = states) +

Could you please check at your end,

 

Thanks and Regards,

 

S.N.V. Krishna

choropleth_labelled.jpeg
Reply all
Reply to author
Forward
0 new messages