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

R ggplot2:更改条形图上堆栈的顺序,r,ggplot2,stacked-chart,facet-wrap,R,Ggplot2,Stacked Chart,Facet Wrap,我试图制作一个带有刻面环绕的堆叠条形图,但我希望我的堆叠变量(“已开发”)的顺序被翻转。我重新排列了因子的顺序,尝试了“order=down(),”以及“scale\u fill\u manual”,但似乎没有任何效果 这是我的密码: developed=rep(c("developed","available"),6) agriculture=rep(c(rep("loi",2), rep("dryland",2), rep("agroforestry",2)),2) acres=c(74

我试图制作一个带有刻面环绕的堆叠条形图,但我希望我的堆叠变量(“已开发”)的顺序被翻转。我重新排列了因子的顺序,尝试了“order=down(),”以及“scale\u fill\u 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
“农业”与“发达”要素水平的变化

然后,绘制:

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)

我想在黑色的“可用”部分之上,再加上灰色的“开发”部分。图例也应与条形图的顺序相匹配


此外,是否可以将图表顶部的“所有岛屿”和“瓦胡岛”从“loi”、“旱地”和“农林业”下的小平面包装移动到底部。谢谢您的帮助

这可能是一个解决方案

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

然后,我们不得不改变传说的颜色和顺序。我无法将我的头环绕在缩放填充灰上,所以我将其改为缩放填充手动,设置值和打断

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轴最近,第二个位于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")
set.seed(123)

testdatFwiw,这里是一个带有
dplyr
的解决方案,它使用
scale\u fill\u 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"))
库(ggplot2)
图书馆(dplyr)
已开发=代表(c(“已开发”、“可用”),6)
农业=代表(c(代表(“loi”,2),代表(“旱地”,2),代表(“农林”,2)),2)
英亩=c(74352425410609120500106517560660379910439085974746893)
岛屿=c(代表(“所有岛屿”,6),代表(“瓦胡岛”,6))
all_is2=数据框架(发达、农业、英亩、岛屿)
全部为2$农业=系数(全部为2$农业,等级=c(“loi”、“旱地”、“农林业”))
#全部2美元已开发=系数(全部2美元已开发,级别=c(“可用”,“已开发”))
所有按(岛屿、农业、发达国家)划分的组别均为3%>%
汇总(英亩=总和(英亩))
ggplot(全部为3,aes(x=农业,y=英亩,填充=开发))+
几何图形栏(position=“stack”,stat=“identity”)+
面_包裹(~岛)+
xlab(“”+ylab(“英亩”)+theme_bw()+
刻度填充手册(名称=”,数值=c(“可用”=“黑色”,“显影”=“浅灰色”))

您需要颠倒因子的顺序。(
levels=c(“可用”、“开发”)
)。如果你想在grid.arrange方法下添加标签,面总是在绘图的顶部或旁边。arrange可能是一种方法。我已经尝试过了,将因子保留在c级(“可用”、“已开发”),但我得到的是可用的面以灰色堆叠在顶部,已开发的面以黑色堆叠在底部。()几乎像是一个bug。在facet方面,感谢@Jota:你可以使用
facet\u wrap(~islands,switch=“x”)
我认为这是相关的。哦,你打败了我!查看
facet\u wrap(~islands,switch=“x”)
以移动下面的facet标签。这是
ggplot2_2.0.0
的一项新功能,您必须通过对数据集排序来控制绘图,这一事实似乎有些奇怪。但是我认为它在内部使用了
dplyr
,所以它可能和我的一样;
order
-美学已经被官方否决,有人建议对数据进行排序以获得相同的效果;你只需要对数据进行排序就可以了。图例的顺序由系数级别的顺序控制;图的顺序按数据的顺序:谢谢大家!我感谢你的帮助@赫罗卡,成功了!非常感谢。不客气,我从一开始就认为这是个好问题。我从中学到了一些东西。很高兴看到你也得到了这么多的选票(其中一张是我的),你现在已经很有名气了。是的,这是我的第一篇帖子,因为它太让人恼火了,我真的好几天都在网上找不到解决办法。但通过发帖很好,我也学到了很多!那里一定是早上。我要结束我的一天:)
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"))