在分面状态图上绘制县名(ggplot2)

时间:2012-04-19 17:37:12

标签: r ggplot2

我正在制作cholorpleth(我开始学习项目HERE)。我曾经问过要在地图上绘制文字(HERE)。我现在正试图在同一张地图上绘制名字,但是它有分面但仍然出错:

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

我认为R恨我并且我是一个愚蠢的人:) traceback长约5英里,所以这也不是一个帮助。如果您取出geom_text,一切运行正常。

PS我知道新的geom_map并且一直在玩这个,但这是一个让我烦恼的单独问题。

提前感谢您的帮助。

#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())

1 个答案:

答案 0 :(得分:7)

在第一个ggplot()调用中,您将组映射到group。然后,此映射会传递到每个图层,因此当ggplot在group图层中使用的centroids数据中找不到geom_text时会抱怨。

groups=NULL来电中使用geom_text取消映射,这很好:

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