How to Make a Grouped Barplot for a Factor with Many Levels

纵然是瞬间 提交于 2019-12-02 08:09:42
Thales

Assuming you want to plot means of Canopy_Index for each Under_Open, Topography cell, you can form means first:

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

Then, plot df.means using the code from your question:

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

Result:

The reason why the bars are currently almost all of the same height is that you overlay multiple values per cell (as pointed out in the comments by Marijn Stevering), effectively plotting the max:

df.max <- aggregate(Canopy_Index ~ Under_Open + Topography, df.melt, max)
# Under_Open         Topography Canopy_Index
# 1   Under_tree Artificial_Surface           75
# 2  Open_Canopy Artificial_Surface           95
# 3   Under_tree          Bare_soil           95
# 4  Open_Canopy          Bare_soil           95
# 5   Under_tree              Grass           95
# 6  Open_Canopy              Grass           95
# 7   Under_tree             Litter           95
# 8  Open_Canopy             Litter           95
# 9   Under_tree        Undergrowth           95
# 10 Open_Canopy        Undergrowth           95
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!