简体   繁体   English

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

[英]Plot county names on faceted state map (ggplot2)

I am working through making cholorpleth's (a learning project I started HERE ). 我正在制作cholorpleth(我从这里开始学习项目)。 I once asked about plotting text on a map ( HERE ) on SO. 我曾经问过在地图上绘制文字( HERE )。 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. 我认为R恨我,我是一个愚蠢的人:) traceback是大约5英里长,所以这也不是一个帮助。 If you take out the geom_text everything runs fine. 如果你拿出geom_text一切都运行正常。

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. PS我知道新的geom_map并且一直在玩这个,但这是一个让我geom_map的单独问题。

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

In the first ggplot() call, you map group to group . 在第一个ggplot()调用中,您将组映射到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. 然后将此映射传递给每个图层,因此当ggplot无法在geom_text图层中使用的centroids数据中找到group时会抱怨。

Unmap it using groups=NULL in the geom_text call, and it is fine: 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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM