2012-04-19 31 views
5

我正在製作cholorpleth's(一個學習項目,我開始了HERE)。我曾經問過關於在地圖上繪製文字(HERE)的問題。現在我想要繪製在同一個地圖上的名字,但它刻面卻不斷收到錯誤:在刻面狀態圖上繪製縣名(ggplot2)

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

我知道,這是不完全最小的,但我知道,分層是GGPLOT2重要,想確認的東西我是以後做不影響'geom_text' – 2012-04-19 17:38:00

回答

7

在第一ggplot()呼叫時,您映射組group。這個映射然後傳遞到每一層,因此當ggplot在geom_text層中使用的centroids數據中找不到group時。

它在geom_text呼叫使用groups=NULL不映射,它是罰款:

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

非常好謝謝:) +1 – 2012-04-19 17:56:58

+0

確實很有幫助,謝謝!感謝w.r.t.的解釋這就是爲什麼當我自己嘗試時它不適合我的原因。 – 2012-06-24 08:39:19