绘制分面状态图上的县名(ggplot2) [英] Plot county names on faceted state map (ggplot2)

查看:161
本文介绍了绘制分面状态图上的县名(ggplot2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过制作cholorpleth(一个学习项目,我开始了 HERE )。我曾经问过在地图上绘制文本(这里)。

  eval(Error)中的错误(错误) expr,envir,enclos):object'group'not found 

我和我是一个imbecile :) traceback 是~5英里长,所以这也不是一个帮助。如果你拿出 geom_text 一切正常。



PS我知道新的 geom_map 并且一直在玩这个,但这是一个单独的问题,正在扰乱我。



非常感谢您的帮助。 / p>

 #从我的保管箱中下载三个数据集
load(url(http://dl.dropbox.com/ (b)(b)(b)(b)(b)(b)(b)(b) ny,centroids= centroids),head)
################################### #################################
#map.data2包含填充信息(测试分数)#
#ny包含绘制边界的纬度和长度信息#
#centroids包含绘制标签的信息#
################# ################################################## #

#加载必需的库
library(ggplot2);库(地图);库(RColorBrewer);库(比例)

ggplot(map.data2,aes(long,lat,group = group))+ #plot合格率数学
geom_polygon(aes(fill = level),color = alpha('white',1/2),size = 0.2)+
geom_polygon(data = ny,color ='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 $ (数据=质心,aes(x = long,y = lat,
label = long,
#y = centroids $ lat,size = 2,color =black)+
geom_text分区域,angle = angle),size = 3)+
opts(title =
New York State Counties Passing Rate \\\
on 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())
ggplot()调用中,将映射组映射到。然后这个映射被传递给每一层,因此当ggplot在 centroids 数据中找不到 group 时在 geom_text 图层中使用。 $ b

geom_text 中使用 groups = NULL 电话,它很好:

  ggplot(map.data2,aes(long,lat,group = group))+ 
geom_polygon(aes(fill = level),color = alpha('white',1/2),size = 0.2)+
geom_polygon(data = ny,color ='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)+#这已更改!
opts(title =
New York State Counties Passing Rate \\\
on 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())


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

解决方案

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

这篇关于绘制分面状态图上的县名(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆