1

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

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

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

您需要样本数据:

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
4

1 回答 1

1

要更改堆积条形图中值的顺序,您应该使用oforder=中的参数并设置排序所需的列名称(在这种情况下)。使用功能,您可以设置条的相反顺序。aes()geom_bar()Responsedesc()

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

在此处输入图像描述

于 2013-04-21T18:40:57.913 回答