2017-01-09 119 views
-1

我有以下barplot其从the data-set 'temp'产生(数据位于此堆栈溢出页面的底部,也可通过上述的此链接找到)。GGPLOT2:添加图例和相关联的图例标题barplot

enter image description here

问题

目标是一个图例添加到barplot(以上)标题为冠层型用标签表示树冠下打开的右手侧冠层

我试着用scale_colour_manual在另一计算器给出的答案,但我不能让一个传奇展现出来。

如果有人可以帮助,那么许多在此先感谢

生成barplot是代码:

假设你想绘制Canopy_Index每个Under_Open的手段,地形单元格,可以形成首先意味着:

df.means <- aggregate(Canopy_Index ~ Under_Open + Topography, df.melt, mean) 

然后,情节df.means使用的代码从你的问题:

ggplot(df.means, aes(x = Topography, y = Canopy_Index, 
        fill = factor(Under_Open), group = Under_Open)) + 
    geom_bar(stat = "identity", position = "dodge") + 
    scale_fill_discrete(name = "Topographical Feature", 
         breaks = c("Under_tree", "Open_Canopy"), 
         labels = c("Under Canopy", "Open Canopy")) + 
    xlab("Topographical Feature") + ylab("Canopy Index") + 
    scale_colour_manual("Canopy Type", values = c("red", "blue")) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1), 
     text = element_text(size=14)) + 
    theme(panel.background = element_blank(), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     panel.border = element_blank()) + 
    theme(axis.line.x =  element_line(color="black", size = 0.8), 
     axis.line.y = element_line(color="black", size = 0.8)) 
+0

在我的机器上运行该代码有传说显示na MED的“地貌形态” – bouncyball

+1

使用你的代码我得到一个传奇,它是由scale_fill_discrete生产。由于aes中没有颜色参数,因此scale_colour_manual不能生成图例。 – Haboryme

+0

很奇怪!运行代码后,图例无法显示。你有什么建议吗? –

回答

0

非常感谢“泰勒斯提供的代码来解决此问题

代码相关联的图例产生barplot:

 ggplot(df.means, aes(x=Topography, y=Canopy_Index,fill=Under_Open)) + 
    geom_bar(stat="identity", position="dodge") + 
    scale_fill_discrete(name="Canopy Type", 
       breaks=c("Under_tree", "Open_Canopy"), 
       labels=c("Under Canopy", "Open Canopy")) + 
    xlab("Topographical Feature") + ylab("Canopy Index") 

Barplot

enter image description here