【问题标题】:Reverse fill order of stacked bars with faceting使用刻面反转堆叠条的填充顺序
【发布时间】:2025-12-24 13:35:07
【问题描述】:

我不知道如何让成交单反转。基本上,我试图让指南和填充匹配单词从正面到负面的内在顺序:

从上到下的指南和成交单应该是:

  • “比我预期的要好得多”,(填充在最顶部,在图例顶部)
  • “比我预期的好一点”,
  • “关于我的预期”,
  • “比我预期的差一点”,
  • “比我预期的要差得多”(填充在最底部,在图例的底部)

您需要样本数据:

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

位置标签

dat <- ddply(dat, .(Banner, Phase), function(x) {
   x$Pos <- (cumsum(x$Proportion) - 0.5*x$Proportion)
   x
})

剧情

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

我尝试过的:

dat$Response <- factor(dat$Response, levels=rev(dat$Response)) 
# No dice, reverses the colour of the scale but not the position of the fill

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    要更改堆积条形图中值的顺序,您应该在 aes()geom_bar() 中使用参数 order=,并设置排序所需的列名称(在本例中为 Response)。使用函数desc(),您可以设置柱的倒序。

    使用您的原始数据框(没有最后一行 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")
    

    为了获得正确的标签放置,更改了位置计算:

    dat <- ddply(dat, .(Banner, Phase), function(x) {
      x$Pos <- (100-cumsum(x$Proportion) + 0.5*x$Proportion)
      x
    })
    

    【讨论】:

    • 标签仍然很不稳定 :( 但我认为你走在正确的轨道上。想想我可能必须在将它注入 ggplot 调用之前这样做。
    • @BrandonBertelsen 通过更改 Pos 计算更新了我的答案