本文介绍了带有小面的堆积条的反向填充顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我无法弄清楚如何让填充顺序反转。基本上,我试图让指南和填充符合单词的内在顺序,从正面到负面: 该指南和填充顺序来自自上而下应该是: 远远超出我的预期,(填充在顶部,位于传说的顶部) 比我预期的要好一点, 关于我的预期, 稍微差一点比我预想的要多, >远远低于我的预期 您需要样本数据: dat< - structure(list(Banner = structure (2L,2L,2L,2L,2L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,1L,1L, 2L, 2L,2L,1L,1L,1L,1L,1L).Label = c(其他,某公司),class =factor),Response = structure (c(1L,2L,3L,4L, 5L,1L,2L,3L,4L,5L,1L,2L,3L,4L,5L,1L,2L,3L,4L,5L, 1L,2L,3L,4L,5L,1L,2L,3L,4 L,5L),。标签= c(远远好于我的预期,比我预期的要好一点,关于我的预期, 比我预期的要差一些,远远超出我的预期),class =factor),Frequency = c(1L, 6L,9L,0L,0L ,29L,71L,149L,32L,6L,1L,7L,16L,1L,0L, 38L,90L,211L,24L,6L,0L,0L,8L,1L,1L,6L,13L,109L ,35L, 9L),比例= c(6,38,56,0,0,10,25,52,11,2,4,28, 64,4,0,10,相位= c(阶段1,阶段1,阶段1,阶段1,阶段1,阶段1,阶段1,阶段1) ,阶段1,阶段1,阶段1,阶段1,阶段1,阶段1,阶段1,阶段2, 阶段2,阶段2,阶段2,阶段2,阶段2,阶段2,阶段2,阶段2,阶段2阶段3,阶段3,阶段3,阶段3,阶段3,阶段3,阶段3,阶段3,阶段3 $ bPhase 3)),.Names = c(Banner,Response,Frequency,Proportion,Phase), row.names = c(NA , sig = character(0), comment =总的来说,我的经验是... by Company,q1 =,q2 =, class = c(survcsub,data.frame)) 位置标签 dat x $ Pos< - (cumsum x $比例) - 0.5 * x $比例)x }) Plot ggplot(dat,aes(Banner,Proportion / 100,fill = Response, label = ifelse > 5,百分比(比例/ 100),)))+ geom_bar(position =fill,stat =identity)+ geom_text(aes(Banner,Pos / 100))+ facet_grid(〜Phase)+ scale_y_continuous(labels = percent)+ labs(x =\\\Company,y =\\\Proportion) 我试过的: dat $ Response< - factor(dat $ Response,levels = rev(dat $ Response))#没有骰子,反转比例的颜色但不填充位置 解决方案要更改堆叠barplot中的值的顺序,您应该使用参数 order = 在 aes()中设置 geom_bar()并设置列的名称订购所需(在这种情况下 Response )。使用函数 desc(),您可以设置条形的逆序。使用原始数据框(不带最后一行) factor())。 ggplot(dat,aes(Banner (比例/ 100),)))+ geom_bar(position =fill,stat =身份,aes(order = desc(Response)))+ geom_text(aes(Banner,Pos / 100))+ facet_grid(〜Phase)+ scale_y_continuous(labels = percent) + labs(x =\\\Company,y =\\\Proportion) $ $ p $ dat x $ Pos x }) I can't figure out how to get the fill order to reverse. Basically, I'm trying to get the guide and the fill to match an intrinsic order of the words from positive to negative: The guide, and the fill order, from top to bottom should be: "Far better than I expected", (Filled at very top, at top of legend)"A little better than I expected", "About what I expected", "A little worse than I expected", "Far worse than I expected" (Filled at very bottom, at bottom of legend)You'll need sample data:dat <- structure(list(Banner = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Other", "Some Company"), class = "factor"), Response = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("Far better than I expected", "A little better than I expected", "About what I expected", "A little worse than I expected", "Far worse than I expected"), class = "factor"), Frequency = c(1L, 6L, 9L, 0L, 0L, 29L, 71L, 149L, 32L, 6L, 1L, 7L, 16L, 1L, 0L, 38L, 90L, 211L, 24L, 6L, 0L, 0L, 8L, 1L, 1L, 6L, 13L, 109L, 35L, 9L), Proportion = c(6, 38, 56, 0, 0, 10, 25, 52, 11, 2, 4, 28, 64, 4, 0, 10, 24, 57, 7, 2, 0, 0, 80, 10, 10, 3, 8, 63, 20, 5), Phase = c("Phase 1", "Phase 1", "Phase 1", "Phase 1", "Phase 1", "Phase 1", "Phase 1", "Phase 1", "Phase 1", "Phase 1", "Phase 2", "Phase 2", "Phase 2", "Phase 2", "Phase 2", "Phase 2", "Phase 2", "Phase 2", "Phase 2", "Phase 2", "Phase 3", "Phase 3", "Phase 3", "Phase 3", "Phase 3", "Phase 3", "Phase 3", "Phase 3", "Phase 3", "Phase 3")), .Names = c("Banner", "Response", "Frequency", "Proportion", "Phase"), row.names = c(NA, 30L), sig = character(0), comment = "Overall, my experience was... by Company", q1 = "", q2 = "", class = c("survcsub", "data.frame"))Position labelsdat <- ddply(dat, .(Banner, Phase), function(x) { x$Pos <- (cumsum(x$Proportion) - 0.5*x$Proportion) x})Plotggplot(dat, aes(Banner, Proportion/100, fill=Response, label=ifelse(Proportion > 5, percent(Proportion/100), ""))) + geom_bar(position="fill", stat="identity") + geom_text(aes(Banner, Pos/100)) + facet_grid(~Phase) + scale_y_continuous(labels=percent) + labs(x="\nCompany", y="\nProportion")What I've tried: dat$Response <- factor(dat$Response, levels=rev(dat$Response)) # No dice, reverses the colour of the scale but not the position of the fill 解决方案 To change the order of values in stacked barplot you should use argument order= in aes() of geom_bar() and set name of column necessary for ordering (in this case Response). With function desc() you can set reverse order of bars.Using your original data frame (without last line of factor()).ggplot(dat, aes(Banner, Proportion/100, fill=Response, label=ifelse(Proportion > 5, percent(Proportion/100), ""))) + geom_bar(position="fill", stat="identity",aes(order=desc(Response))) + geom_text(aes(Banner, Pos/100)) + facet_grid(~Phase) + scale_y_continuous(labels=percent) + labs(x="\nCompany", y="\nProportion")To get correct placement of labels, changed calculation of positions:dat <- ddply(dat, .(Banner, Phase), function(x) { x$Pos <- (100-cumsum(x$Proportion) + 0.5*x$Proportion) x}) 这篇关于带有小面的堆积条的反向填充顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 11:07