5

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())
Community
  • 1
  • 1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • I know that this isn't exactly minimal but I know that layering is important in ggplot2 and wanted to make sure something I was doing later on wasn't affecting the `geom_text` – Tyler Rinker Apr 19 '12 at 17:38

1 Answers1

7

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())
joran
  • 169,992
  • 32
  • 429
  • 468
smu
  • 8,757
  • 2
  • 19
  • 14
  • Very helpful indeed, thank you! Thanks for that explanation w.r.t. `group` that was the reason why it did not work for me when I tried on my own. – Matt Bannert Jun 24 '12 at 08:39