【问题标题】:ggplot2: Changing the order of stacks on a bar graphggplot2:更改条形图上的堆栈顺序
【发布时间】:2016-04-15 10:47:37
【问题描述】:

我正在尝试使用 facet_wrap 制作堆叠条形图,但我希望翻转堆叠变量(“已开发”)的顺序。我重新排序了因子,并尝试了“order=descend()”和“scale_fill_manual”,但似乎没有任何效果。

这是我的代码:

developed=rep(c("developed","available"),6)
agriculture=rep(c(rep("loi",2), rep("dryland",2), rep("agroforestry",2)),2)  
acres=c(7435,24254,10609,120500,10651,75606,6037,9910,4390,895,9747,46893)
islands=c(rep("All islands",6), rep("Oahu",6))
all_is2=data.frame(developed, agriculture, acres, islands)
head(all_is2)
  developed  agriculture  acres      island
1 developed          loi   7435 All islands
2 available          loi  24254 All islands
3 developed      dryland  10609 All islands
4 available      dryland 120500 All islands
5 developed agroforestry  10651 All islands
6 available agroforestry  75606 All islands

“农业”和“发达”的因素水平变化

all_is2$agriculture=factor(all_is2$agriculture,levels=c("loi","dryland","agroforestry"))
all_is2$developed=factor(all_is2$developed,levels=c("developed","available"))
levels(all_is2$developed)
[1] "developed" "available"

然后,绘图:

ggplot(all_is2,aes(x=agriculture,y=acres,fill=developed))+
     geom_bar(position="stack", stat="identity")+
     facet_wrap(~islands)+ scale_fill_grey(start=0.8, end=0.2, name="")+ xlab("")+ylab("Acres")+theme_bw()+ scale_y_continuous(labels=comma)

我希望在条形的“可用”部分(黑色)之上的灰色条形“已开发”部分。并且图例也应该与条形图的顺序相匹配。

此外,是否可以将顶部的 facet_wrap“所有岛屿”和“欧胡岛”移动到“loi”“旱地”和“农林业”下的图表底部。谢谢你的帮助!!

【问题讨论】:

  • 你需要颠倒你的因素的顺序。 (levels=c("available","developed"))。并且方面总是在图的顶部或之外,如果你想要在 grid.arrange 方法下方的标签可能是要走的路。
  • 我已经尝试过了,将因子保留在 levels=c("available"," Developed"),但我得到的是可用的堆叠在顶部的灰色和开发的堆叠在底部黑色的。 (imgur.com/ne3iNhk)
  • 几乎看起来像一个错误。
  • 在方面,感谢@Jota:您可以使用facet_wrap(~islands , switch = "x")
  • 我认为this issue on ggplot github 是相关的。

标签: r ggplot2 stacked-chart facet-wrap


【解决方案1】:

这可能是一个解决方案。

我所做的是对数据集进行排序,以便我希望最接近 x 轴的值首先出现在数据集中。 (我在这里使用了您对因素的排序)。这会固定条的位置。

然后,我们必须更改图例的颜色和顺序。我无法理解 scale_fill_grey,所以我将其改为 scale_fill_manual,同时设置值和中断。

ggplot(all_is2[rev(order(all_is2$developed)),] ,aes(x=agriculture,y=acres,fill=developed))+
  geom_bar(position="stack", stat="identity")+theme_bw()+
  facet_wrap(~islands)+ 
  scale_fill_manual(values=c(developed="grey80",available="grey20"),name="",
                    breaks=c("developed","available"))+
 xlab("")+ylab("Acres")

我不知道这是一个错误还是一个功能,我认为这也发生在 ggplot 中的早期版本中,但似乎使用 stat_identity 第一个观察值绘制在最靠近 x 轴的位置,第二个在顶部等等。

演示:

set.seed(123)
testdat <- data.frame(x=1,y=sample(5))


p1 <- ggplot(testdat, aes(x=x,y=y,fill=factor(y))) +geom_bar(stat="identity")+labs(title="order in dataset")
p2 <- ggplot(testdat[order(testdat$y),],aes(x=x,y=y,fill=factor(y))) +
  geom_bar(stat="identity") + labs(title="ordered by y")
p3 <- ggplot(testdat[rev(order(testdat$y)),],aes(x=x,y=y,fill=factor(y))) +
  geom_bar(stat="identity") + labs(title="reverse ordered by y")

【讨论】:

  • 哦,你打败了我!查看facet_wrap(~islands , switch = "x") 以移动下面的分面标签。这是ggplot2_2.0.0的新功能
  • 您必须通过对数据集进行排序来控制绘图的事实似乎很奇怪。但是我以为它内部使用了dplyr,所以可能和我的一样。
  • 我用的是ggplot2.0; order-aesthetic 已被正式弃用,有人建议订购数据以获得相同的效果。
  • 你实际上让它比它需要的复杂一点;您只需要订购数据即可。图例的顺序由因子水平的顺序控制;按数据顺序出图的顺序:ilari.scheinin.fi/ggplot-2-0-and-the-missing-order-aesthetic
  • 谢谢大家!我感谢您的帮助! @Heroka 成功了!谢谢!
【解决方案2】:

Fwiw,这是dplyr 的解决方案,它使用scale_fill_manual 来明确颜色:

library(ggplot2)
library(dplyr)

developed=rep(c("developed","available"),6)
agriculture=rep(c(rep("loi",2), rep("dryland",2), rep("agroforestry",2)),2)  
acres=c(7435,24254,10609,120500,10651,75606,6037,9910,4390,895,9747,46893)
islands=c(rep("All islands",6), rep("Oahu",6))
all_is2=data.frame(developed, agriculture, acres, islands)

all_is2$agriculture=factor(all_is2$agriculture,levels=c("loi","dryland","agroforestry"))
#all_is2$developed=factor(all_is2$developed,levels=c("available","developed"))

all_is3 <- all_is2 %>% group_by(islands,agriculture,developed) %>% 
                       summarize(acres=sum(acres)) 

ggplot(all_is3,aes(x=agriculture,y=acres,fill=developed))+
  geom_bar(position="stack", stat="identity")+
  facet_wrap(~islands)+ 
  xlab("")+ylab("Acres")+theme_bw() +
  scale_fill_manual(name="",values=c("available"="black","developed"="light gray"))

【讨论】:

  • 欢迎您,我从一开始就认为这是一个很好的问题。我从中学到了一些东西。很高兴看到你也得到了所有这些赞成票(一个是我的),你现在已经很成熟了。
  • 是的,这是我的第一篇文章,因为它太令人恼火了,我真的好几天都找不到在线解决方案。不过发帖很好,我也学到了很多!
  • 那里一定是早上。我要结束我的一天了:)
猜你喜欢
  • 2021-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 2019-05-19
  • 2012-03-02
  • 1970-01-01
  • 2020-08-24
相关资源
最近更新 更多