ggplot2:更改条形图上堆栈的顺序 [英] ggplot2: Changing the order of stacks on a bar graph

查看:216
本文介绍了ggplot2:更改条形图上堆栈的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个带有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)
英亩= c(7435,24254,10609,120500,10651,75606, 6037,9910,4390,895,9747,46893)
islands = c(rep(所有岛屿,6),rep(瓦胡岛,6))
all_is2 = data.frame(开发,农业,英亩,岛屿)
头(all_is2)
发达的农业亩岛
1发达的loi 7435所有的岛
2可利用的loi 24254所有的岛
3发达的旱地10609所有岛屿
4可用旱地120500所有岛屿
5已开发农林业10651所有岛屿
6可用农林业75606所有岛屿

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

  all_is2 $农业=因子(all_is2 $ agriculture,levels = c(loi,dryland,agroforestry))
all_is2 $ developed = factor(all_is2 $ developed,levels = c(developed,available)) )
等级(all_is2 $开发)
[1]开发可用



然后,绘图:

  ggplot(all_is2,aes(x =农业,y =英亩,填充=已开发)) + 
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_wrapAll islands和Oahu移动到顶部loi,dryland和agroforestry下的图表底部。感谢您的帮助!!

解决方案



我所做的是对数据集进行排序,以便我想要显示的最接近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 =数据集中的顺序)
p2 < - ggplot(testdat [order(testdat $ y),],aes(x = x,y = y,fill = factor(y)))+
geom_bar(stat =identity)+ labs (title =order by y)
p3 < - ggplot(testdat [rev(order(testdat $ y)),],aes(x = x,y = y,fill = factor(y)) )+
geom_bar(stat =identity)+ labs(title =按y排序)


I'm trying to make a stacked bar graph with a facet_wrap, but I want the order of my stacked variables ("developed") to be flipped. I've reordered the factors, and tried "order=descend()," as well as "scale_fill_manual" and nothing seems to work.

Here is my code:

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

changing factor levels of "agriculture" and "developed"

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"

Then, plotting:

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)

I want the "developed" parts of the bars in gray on top of the "available" parts of the bars, which are black. And the legend should match the order of the bars as well.

Also, is it possible to move the facet_wrap "All islands" and "Oahu" at the top to the bottom of the graph under "loi" "dryland" and "agroforestry." Thank you for your help!!

解决方案

This might be a solution.

What I did was ordering the dataset, so that the value I wanted to appear closest to the x-axis appeared first in the dataset. (I've used your ordering of factors here). This fixt the positioning of the bars.

Then, we had to change the colors and order of the legend. I could not wrap my head around scale_fill_grey, so I changed it to scale_fill_manual instead, setting both values and breaks.

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")

I don't know if it's a bug or a feature, and I think this also happened with previous versions in ggplot, but it appears that with stat_identity the first observation is plotted closest to the x-axis, the second on top of that etc.

Demonstration:

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")

这篇关于ggplot2:更改条形图上堆栈的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆