faceting a map

31 views
Skip to first unread message

Tyler Rinker

unread,
Feb 10, 2012, 12:51:18 AM2/10/12
to ggplot
Greetings,

I am fairly new to ggplot2 and am trying to work through some more advanced mapping features (I geocoded data and plotted it onto a map for the first time this week).  One thing I'd like to be able to do for visualization is faceting with the facet_grid argument.  I tried this and failed miserably.  I thought I'd go find some help on it but I can't even find a google image of it.  Is this possible?  

Often hotmail or ,perhaps user malfunction, has a proclivity towards poorly formatted messages so I provided the code in a pastebin link as well.

##################################################
p <- ggplot()
p <- p + geom_polygon( data=states, aes(x=long, y=lat, group = group),
         colour="white" )
p <- p + geom_point(data=mydata, hjust=0.5, vjust=-0.5, aes(x=long, y=lat, 
         label=label), colour="gold2", size=4 )
p  + facet_grid(liberal_arts~.)# massive fail angry message
#Error: Casting formula contains variables not found in molten data: liberal_arts

#It works if you take the map out but I want to keep it in
p <- ggplot()
p <- p + geom_point(data=mydata, hjust=0.5, vjust=-0.5, aes(x=long, y=lat, 
         label=label), colour="gold2", size=4 )
p  + facet_grid(liberal_arts~.)
##################################################

R 2.14.1 (2011-12-22); windows OS


Cheers
Tyler

James McCreight

unread,
Feb 10, 2012, 1:06:48 AM2/10/12
to Tyler Rinker, ggplot
Hi Tyler-

without the data this is speculative

p <- ggplot( mydata, aes(x=long, y=lat, label=label) ) +
        geom_point( hjust=0.5, vjust=-0.5,  colour="gold2", size=4 ) +
        geom_polygon( data=states, aes(x=long, y=lat, group = group),
         colour="white" ) +
        facet_grid(liberal_arts~.)

if that dosent work you can send the data

dput(mydata)
dput(states)

but I'd assume they are quite long, at least the latter.

James

--
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



--
- 
******************************************************************************
James McCreight                               
cell: (831) 261-5149
VoIP (to cell): (720) 897-7546

Tyler Rinker

unread,
Feb 10, 2012, 1:57:53 AM2/10/12
to mccr...@colorado.edu, ggplot

What a terrible oversight on the data.  It's actually an example from:


From: mccr...@colorado.edu
Date: Thu, 9 Feb 2012 23:06:48 -0700
Subject: Re: faceting a map
To: tyler_...@hotmail.com
CC: ggp...@googlegroups.com

Dennis Murphy

unread,
Feb 10, 2012, 1:55:37 AM2/10/12
to Tyler Rinker, ggplot
Hi Tyler:

Are you trying to construct a choropleth map? If so, then you should
probably install the development version of ggplot2-0.9.0 and consider
using geom_map() in the new version (it doesn't exist in 0.8.9). If
that's what you're after, let us know and we can show you how to
install it and get moving with it. The geom_map() help page and my
0.9.0 transition guide (which isn't quite ready for release to this
group yet but will probably be by next week) have an example using a
US map with a choropleth plot.

HTH,
Dennis

Tyler Rinker

unread,
Feb 10, 2012, 2:00:17 AM2/10/12
to mccr...@colorado.edu, ggplot
Sorry a fishy oddity with hotmail.  I appreciate your patience.

What a =n oversight with the missing data.  My apologies.  It's actually an example from:

and can be read in with the url function.

############################################################
library(ggplot2)

mydata <- read.csv(file=url("http://dl.dropbox.com/u/6517578/tmp.csv"), header=TRUE, row.names=1, sep=",")
all_states <- map_data("state")


states <- subset(all_states, region %in% c( "illinois", "indiana", "iowa", 
         "kentucky", "michigan", "minnesota","missouri", "north dakota", "ohio", "south dakota", 
         "wisconsin" ))

p <- ggplot()
p <- p + geom_polygon( data=states, aes(x=long, y=lat, group = group),
         colour="white" )
p <- p + geom_point(data=mydata, hjust=0.5, vjust=-0.5, aes(x=long, y=lat, 
         label=label), colour="gold2", size=4 )
p  + facet_grid(liberal_arts~.)# massive fail angry message
#Error: Casting formula contains variables not found in molten data: liberal_arts

#It works if you take the map out but I want to keep it in
p <- ggplot()
p <- p + geom_point(data=mydata, hjust=0.5, vjust=-0.5, aes(x=long, y=lat, 
         label=label), colour="gold2", size=4 )
p  + facet_grid(liberal_arts~.)
############################################################ 

From: tyler_...@hotmail.com
To: mccr...@colorado.edu
CC: ggp...@googlegroups.com
Subject: RE: faceting a map
Date: Fri, 10 Feb 2012 01:57:53 -0500

Dennis Murphy

unread,
Feb 10, 2012, 3:25:59 AM2/10/12
to Tyler Rinker, mccr...@colorado.edu, ggplot
Hi Tyler:

The problem is that when you call facet_wrap(), there's no referent
data frame for liberal_arts. One way to resolve the problem is shown
below. I played around with a few other things, too, including aspect
ratios. Take the opts() code out and see what happens :)

library('ggplot2')
library('maps')
library('maptools') # you forgot these...

mydata <- read.csv(file=url("http://dl.dropbox.com/u/6517578/tmp.csv"),
header=TRUE, row.names=1, sep=",")
all_states <- map_data("state")

# Don't know why you included the Dakotas since there are no points
# in either state - you could remove them WLOG and enlarge the faceted
# maps a bit


states <- subset(all_states, region %in% c( "illinois", "indiana", "iowa",
"kentucky", "michigan", "minnesota","missouri", "north dakota",
"ohio", "south dakota", "wisconsin" ))

# Use mydata as the data argument so that liberal_arts
# has a referent data frame when invoked
ggplot(mydata) +


geom_polygon( data=states, aes(x=long, y=lat, group = group),

fill = 'grey 40', colour="skyblue" ) +
geom_point(aes(x=long, y=lat, label=label),
hjust=0.5, vjust=-0.5, colour="gold2", size=3 ) +
opts(aspect.ratio = 1) +
facet_wrap(~ liberal_arts, ncol = 1)

In 0.9.0, the extended spacing between the vertical axes and the axis
label is fixed. (See the attachments.)

The original blog post was fine because the poster didn't try to facet
by a variable - in that case, the ggplot() call with no arguments was
fine since each subsequent layer defined the data frame to be used in
that layer. When you decided to experiment with faceting, you
neglected to realize that liberal_arts had no reference for ggplot()
to use, hence the error. Once you put mydata in the ggplot() call as
above, facet_wrap() can point back to it and retrieve the liberal_arts
variable.

Dennis

map090.pdf
map089.pdf
Reply all
Reply to author
Forward
0 new messages