Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot county names on faceted state map (ggplot2)

Tags:

r

ggplot2

I am working through making cholorpleth's (a learning project I started HERE). I once asked about plotting text on a map (HERE) on SO. I'm now trying to plot names on the same map but with it faceted but keep getting an error:

Error in eval(expr, envir, enclos) : object 'group' not found

Which I take to me that R hates me and I'm an imbecile :) traceback is ~ 5 miles long so that's not a help either. If you take out the geom_text everything runs fine.

PS I know about the new geom_map and have been playing with that as well but this is a separate problem that's bugging me.

Thank you in advance for your help.

#Load three data sets from my dropbox 
load(url("http://dl.dropbox.com/u/61803503/Names/cholo.RData"))

#view head of the three data frames
lapply(list("map.data2"=map.data2, "ny"=ny, "centroids"=centroids), head)
####################################################################
# map.data2 contains the filling information (test scores)         #
# ny contains the lat and long information for plotting boundaries #
# centroids contains the information for plotting labels           #
####################################################################

#Load Necessary Libraries
library(ggplot2); library(maps); library(RColorBrewer); library(scales)

ggplot(map.data2, aes(long, lat, group=group)) +  #plot pass rates math
   geom_polygon(aes(fill=level), colour=alpha('white', 1/2), size=0.2) +
   geom_polygon(data=ny, colour='black', fill=NA) + 
   scale_fill_brewer(palette='RdYlBu', guide = guide_legend(title = 
       "Percent Passing"))+
   facet_grid(.~Subject)+
   #annotate(data = "text", label = centroids$subregion, x = centroids$long, 
   #    y = centroids$lat, size = 2, colour = "black") +
   geom_text(data=centroids, aes(x=long, y=lat, 
       label=subregions, angle=angle), size=3) +
   opts(title = "
       New York State Counties Passing Rate \non Elementary ELA Assessments") +
   opts(axis.text.x = theme_blank(), axis.text.y = theme_blank(), 
       axis.ticks = theme_blank())+
   opts(legend.background = theme_rect()) +
   scale_x_continuous('') + scale_y_continuous('') + 
   labs(title = "legend title") + theme_bw()+
   opts(axis.line=theme_blank(),axis.text.x=theme_blank(),
        axis.text.y=theme_blank(),axis.ticks=theme_blank(),
        axis.title.x=theme_blank(), legend.position="bottom",
        axis.title.y=theme_blank(),
        panel.background=theme_blank(),panel.grid.major=theme_blank(),
        panel.grid.minor=theme_blank(),plot.background=theme_blank())
like image 817
Tyler Rinker Avatar asked Apr 19 '12 17:04

Tyler Rinker


1 Answers

In the first ggplot() call, you map group to group. This mapping is then passed on to each layer, therefore ggplot complains when it can't find group in the centroids data used in your geom_text layer.

Unmap it using groups=NULL in the geom_text call, and it is fine:

ggplot(map.data2, aes(long, lat, group=group)) +  
   geom_polygon(aes(fill=level), colour=alpha('white', 1/2), size=0.2) +
   geom_polygon(data=ny, colour='black', fill=NA) + 
   scale_fill_brewer(palette='RdYlBu', guide = guide_legend(title = 
         "Percent Passing"))+
   facet_grid(.~Subject)+
   geom_text(data=centroids, aes(x=long, y=lat, 
     label=subregion, angle=angle, group=NULL), size=3) +   # THIS HAS CHANGED!
   opts(title = " 
     New York State Counties Passing Rate \non Elementary ELA Assessments") +
   opts(axis.text.x = theme_blank(), axis.text.y = theme_blank(), 
       axis.ticks = theme_blank())+
   opts(legend.background = theme_rect()) +
   scale_x_continuous('') + scale_y_continuous('') + 
   labs(title = "legend title") + theme_bw()+
    opts(axis.line=theme_blank(),axis.text.x=theme_blank(),
      axis.text.y=theme_blank(),axis.ticks=theme_blank(),
      axis.title.x=theme_blank(), legend.position="bottom",
      axis.title.y=theme_blank(),
      panel.background=theme_blank(),panel.grid.major=theme_blank(),
      panel.grid.minor=theme_blank(),plot.background=theme_blank())
like image 58
smu Avatar answered Sep 25 '22 18:09

smu