重新订购堆叠式条形图ggplot2

时间:2019-10-22 18:45:18

标签: r ggplot2 geom-bar

我有一个已绘制为堆积条形图的数据集。这是一个看起来像的例子:

df = data.frame(bay = c("A", "A", "A", "B", "B", "C", "C", "C"), species = c("snook","tarpon","snapper","tarpon","redfish","snook","snapper","redfish"), mean=c(5,7,2,1,5,8,4,10))

我能够以我希望的方式绘制它:

ggplot(df, aes(fill=bay, y=mean, x=species)) + 
  geom_bar(position="stack", stat="identity")

我得到以下情节:

ggplot output

我的问题是我想对x轴进行重新排序,以使物种从最高总体均值(跨海湾)变为最低总体均值,因此顺序应为红鱼,斯诺克,塔彭,鲷鱼。是否有捷径可寻?谢谢!

0 个答案:

没有答案