在ggplot2中以连续的比例添加NA

时间:2014-04-28 13:50:16

标签: r ggplot2 legend na

我在R中创建了这个地理图表,并且在绘制图例中的NA值(黑色区域)时遇到了问题

Imgur 这是我用于映射的代码:

map<-ggplot(penetration_levels,aes(long,lat,group=region,fill=Penetration,))+geom_polygon()+coord_equal()+scale_fill_gradient2(low="Red",mid="white",high="Green",midpoint=.33,na.value="Black",label=percent)
map<-map+geom_point(data=mydata, aes(x=long, y=lat,group=1,fill=0, size=Annualized.Opportunity),color="gray6") + scale_size(name="Total Annual Opportunity-Millions",range=c(1,6))  
map<-map+borders("state", colour="black", alpha=0.8)
map<-map+theme(plot.title = element_text(size = 12))
map<-map+theme_bw()+theme(plot.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.border = element_blank())
map

我尝试了一些使用map + legend()的方法并尝试了不同的方法来添加NAs作为美学,但我遇到了困难,主要问题是用户错误。

基本上我要做的是在存在填充框的情况下添加一个图例并说“没有可用的信息”#34;

合并代码:

states<-merge(states,statelookup,by="region",all.x=T)
states<-states[order(states$order),]
penetration_levels<-merge(states,penetration_levels,by="State",all.x=T)
penetration_levels<-penetration_levels[order(penetration_levels$order),]

变量元首:

head(penetration_levels)
   State  region      long      lat group order subregion state    From      To   Total     Penetration
23    AL alabama -87.46201 30.38968     1     1      <NA>    AL 3104873 2691875 5796748    0.4643768
24    AL alabama -87.48493 30.37249     1     2      <NA>    AL 3104873 2691875 5796748    0.4643768
53    AL alabama -87.52503 30.37249     1     3      <NA>    AL 3104873 2691875 5796748   0.4643768
54    AL alabama -87.53076 30.33239     1     4      <NA>    AL 3104873 2691875 5796748   0.4643768
55    AL alabama -87.57087 30.32665     1     5      <NA>    AL 3104873 2691875 5796748   0.4643768
56    AL alabama -87.58806 30.32665     1     6      <NA>    AL 3104873 2691875 5796748   0.4643768

head(mydata)
   Sold.To.Customer         City State From.To Annualized.Opportunity        location       lat      long
16426          10000110     NEW YORK    NY    FROM               13.39604     NEW YORK,NY 40.71435 -74.00597
117702         10016487 INDEPENDENCE    OH    FROM               12.99607 INDEPENDENCE,OH 41.36866 -81.63790
165397         11001422      DETROIT    MI    FROM               11.37319      DETROIT,MI 42.33143 -83.04575
13322          10000096  SAINT LOUIS    MO    FROM               10.79246  SAINT LOUIS,MO 38.62700 -90.19940
224992         11067228      HOUSTON    TX    FROM               10.69957      HOUSTON,TX 29.76019 -95.36939
101902         10014909    MANHASSET    NY    FROM               10.59856    MANHASSET,NY 40.79788 -73.69957

0 个答案:

没有答案