首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ggplot2:更改条形图上堆栈的顺序

ggplot2:更改条形图上堆栈的顺序
EN

Stack Overflow用户
提问于 2016-01-11 15:32:29
回答 2查看 5K关注 0票数 12

我正在尝试用facet_wrap制作堆叠条形图,但我希望我的堆叠变量(“显影”)的顺序被颠倒。我重新排序了因子,并尝试了"order=descend()“和"scale_fill_manual”,但似乎都不起作用。

下面是我的代码:

代码语言:javascript
复制
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

改变“农业”和“发达”的要素水平

代码语言:javascript
复制
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"

然后,绘制:

代码语言:javascript
复制
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“”旱地“和”农林复合“下的底部。谢谢你的帮助!!

EN

回答 2

Stack Overflow用户

发布于 2016-01-11 16:27:14

这可能是一个解决方案。

我所做的是对数据集进行排序,以便我希望出现在最靠近x轴的值出现在数据集中的第一个位置。(我在这里使用了您对因子的排序)。这将固定条形图的位置。

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

代码语言:javascript
复制
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")

我不知道这是一个bug还是一个特性,我认为在ggplot的以前版本中也发生了这种情况,但似乎在stat_identity中,第一个观察值绘制在最靠近x轴的位置,第二个观察值绘制在最上面的位置,等等。

演示:

代码语言:javascript
复制
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")

票数 11
EN

Stack Overflow用户

发布于 2016-01-11 16:41:26

顺便说一下,这是一个使用dplyr的解决方案,它使用scale_fill_manual来明确表示颜色:

代码语言:javascript
复制
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"))

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34716245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档