【问题标题】:Make a 2D legend for a plot - bi-variate choropleth maps为绘图制作 2D 图例 - 双变量等值线图
【发布时间】:2015-09-23 21:33:23
【问题描述】:

我一直在玩双变量 choropleth 地图,并且一直坚持如何创建类似于此处显示的 Joshua Stevens 的 2d 图例:

为了说明挑战,我们不需要使用地图。下面的代码就足够了:

#test desired legend appearance
library(ggplot2)
library(reshape2)
#use color scheme shown here http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/
bvColors=c("#be64ac","#8c62aa","#3b4994","#dfb0d6","#a5add3","#5698b9","#e8e8e8","#ace4e4","#5ac8c8")
legendGoal=melt(matrix(1:9,nrow=3))
test<-ggplot(legendGoal, aes(Var1,Var2))+ geom_tile(aes(fill = as.factor(value)))
test<- test + scale_fill_manual(name="Var1 vs Var2",values=bvColors,drop=FALSE)
test

它创建了一个看起来像我想要的图例的情节,但当然图例是所有级别的垂直条。我希望图例看起来像情节本身。有没有办法做到这一点?谢谢!

【问题讨论】:

    标签: r plot ggplot2 gis


    【解决方案1】:
    #test desired legend appearance
    library(ggplot2)
    library(reshape2)
    #use color scheme shown here http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/
    bvColors=c("#be64ac","#8c62aa","#3b4994","#dfb0d6","#a5add3","#5698b9","#e8e8e8","#ace4e4","#5ac8c8")
    melt(matrix(1:9,nrow=3))
    legendGoal=melt(matrix(1:9,nrow=3))
    test<-ggplot(legendGoal, aes(Var2,Var1,fill = as.factor(value)))+ geom_tile()
    test<- test + scale_fill_manual(name="More Var2  -->",values=bvColors,drop=FALSE)
    test<-test+guides(fill = guide_legend(nrow = 3))
    test<-test + theme(legend.text=element_blank())
    
    test
    

    剩下的唯一技巧是想办法在“More Var1 -->”的图例一侧添加一些竖线。这是一种非常丑陋的做法:

    test<-ggplot(legendGoal, aes(Var2,Var1,fill = as.factor(value)))+ geom_tile()
    test<- test + scale_fill_manual(name="More Var2  -->",values=bvColors,labels=c("","","","","","","More","Var 1"," v "))
    test<-test+guides(fill = guide_legend(nrow = 3))
    #test<-test + theme(legend.text=element_blank())
    
    test
    

    但是,正如 zx 所示,使用 cowplot 包扩展 ggplot2 是完整的解决方案:

    #test desired legend appearance
    library(ggplot2)
    library(cowplot)
    library(reshape2)
    #use color scheme shown here http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/
    bvColors=c("#be64ac","#8c62aa","#3b4994","#dfb0d6","#a5add3","#5698b9","#e8e8e8","#ace4e4","#5ac8c8")
    melt(matrix(1:9,nrow=3))
    legendGoal=melt(matrix(1:9,nrow=3))
    test<-ggplot(legendGoal, aes(Var2,Var1,fill = as.factor(value)))+ geom_tile()
    test<- test + scale_fill_manual(name="",values=bvColors)
    test<-test+guides(fill = guide_legend(nrow = 3))
    test<-test + theme(legend.text=element_blank())
    test<-ggdraw(test) + draw_text(text = "More Var 2 -->",x=0.91,y=0.58)
    test<-ggdraw(test) + draw_text(text = "More Var 1 -->",x=0.84,y=0.5,angle=270)
    test
    

    只是为了好玩,这是我用这种技术制作的地图:

    【讨论】:

      【解决方案2】:

      不是一个完整的答案,但我认为您需要使用guides

      ggplot(legendGoal, 
             aes(Var1,Var2,
                 col=as.factor(value),
                 fill=as.factor(value))) +
        geom_tile() +
        guides(col = guide_legend(nrow = 3))
      

      【讨论】:

      • 指南!那是门票和我没有探索过的 ggplot2 区域。您的解决方案几乎就在那里。 “col”与“fill”相反,在您的示例中排除了使用手动颜色。在经过一些排列后,我想出了下面的解决方案。谢谢!
      【解决方案3】:

      好的,最后一次更新。列之间的空白让我很烦,cowplot 的网格功能将让我们使用缩小的图作为我们的双变量图例,如下所示:

      library(ggplot2)
      library(cowplot)
      library(reshape2)
      #use color scheme shown here http://www.joshuastevens.net/cartography/make-a-bivariate-choropleth-map/
      bvColors=c("#be64ac","#8c62aa","#3b4994","#dfb0d6","#a5add3","#5698b9","#e8e8e8","#ace4e4","#5ac8c8")
      melt(matrix(1:9,nrow=3))
      legendGoal=melt(matrix(1:9,nrow=3))
      test<-ggplot(legendGoal, aes(Var2,Var1,fill = as.factor(value)))+ geom_tile()
      test<- test + scale_fill_manual(name="",values=bvColors)
      test<-test + theme(legend.position="none")
      #test<-ggdraw(test) + draw_text(text = "More Var 2 -->",x=0.91,y=0.58)
      #test<-ggdraw(test) + draw_text(text = "More Var 1 -->",x=0.84,y=0.5,angle=270)
      
      #create a plot that will be the legend itself
      lg<- test #would not be true when making a map
      lg<-lg + theme(axis.title.x=element_text(size=rel(1),color=bvColors[3])) + xlab("More Var2 -->")
      lg<-lg + theme(axis.title.y=element_text(size=rel(1),color=bvColors[3])) + ylab("More Var1 -->")
      lg<-lg+theme(axis.text=element_blank())
      lg<-lg+theme(line=element_blank())
      #put both plots on a grid
      ggdraw()+ draw_plot(lg,0.1,0.7,width=0.2,height=0.2) +draw_plot(test,0.3,0,width=.7,height=.7)
      

      漂亮,是吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多