按照其中一个变量的值设置堆叠条形图的顺序

6
我被要求制作一个堆积条形图,其中的条和值按照精确的方式堆叠和排序。
在本例中,“A3”在左侧,“A2”在中间,“A1”在右侧,我已经完成了这个要求。
问题在于,我还被要求按“A1”的值的降序顺序对条进行排序。在本例中,这意味着“Value 11”出现在顶部,按顺序降序排列到“Value 6”。条的排序记录在向量“plotOrder”中,我尝试使用它在下面的代码中实现一些东西。
在SO上搜索有关排序堆积条形图的许多问题中,我没有找到一个问题或答案描述或解决这个特定的问题。非常感谢您的任何帮助。
下面的简化代码下载了一些随机数据,其形式与我目前正在使用的相同,并生成了以下图表。
library(ggplot2)

stackedBarPlot <- 
        ggplot(data) +
        aes(x = ValueName, y = Percent, fill = reorder(Response, plotOrder)) +
        geom_bar(position = "fill", stat = "identity") +
        coord_flip()

这将生成此图表:

Where

数据:

structure(list(ValueName = c("Value 11", "Value 14", "Value 13", 
"Value 12", "Value 10", "Value 1", "Value 5", "Value 9", "Value 4", 
"Value 7", "Value 8", "Value 3", "Value 15", "Value 2", "Value 6", 
"Value 11", "Value 14", "Value 13", "Value 12", "Value 10", "Value 1", 
"Value 5", "Value 9", "Value 4", "Value 7", "Value 8", "Value 3", 
"Value 15", "Value 2", "Value 6", "Value 11", "Value 14", "Value 13", 
"Value 12", "Value 10", "Value 1", "Value 5", "Value 9", "Value 4", 
"Value 7", "Value 8", "Value 3", "Value 15", "Value 2", "Value 6"
), plotOrder = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L), Response = c("A1", "A1", "A1", 
"A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", 
"A1", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", "A2", 
"A2", "A2", "A2", "A2", "A2", "A3", "A3", "A3", "A3", "A3", "A3", 
"A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3", "A3"), Percent = c(86.5, 
85.3, 84.5, 83.2, 81, 79.5, 77, 76.7, 71, 66.2, 64.5, 60.5, 59.6, 
57.2, 53.2, 9.9, 9.4, 10.2, 9.9, 11.8, 14.7, 13.9, 13.5, 15.1, 
16.1, 21.3, 21.3, 26.6, 19.8, 18.5, 3.6, 5.3, 5.3, 6.9, 7.2, 
5.8, 9, 9.8, 13.9, 17.7, 14.1, 18.2, 13.8, 22.9, 28.2)), .Names = c("ValueName", 
"plotOrder", "Response", "Percent"), row.names = c(NA, -45L), class = c("tbl_df", 
"tbl", "data.frame"))

非常感谢您提供任何建议或解决方案。


1
很少见到有人如此关注代码的编写方式(在我看来非常漂亮)。您能否使其自包含(即没有链接到外部资源)? - Roman Luštrik
我已经添加了数据并简化了代码以反映,希望这样可以。 - Axeman
2个回答

7

您已经有了plotOrder,但需要将其应用于重新排序x而不是fill...

stackedBarPlot <- 
  ggplot(data) +
  aes(x = reorder(ValueName,-plotOrder), 
      y = Percent, 
      fill = Response) +
  geom_bar(position = "fill", stat = "identity") +
  coord_flip()

enter image description here


1
感谢 @Andrew Gustar,这是一个优雅的解决方案。很好地解决了问题。非常感谢! - vengefulsealion
@Andrew Gustar,非常好的解决方案!您有没有建议如何修改您的代码以实现相同的结果,但不需要plotOrder列? - M.Teich
4
@M.Teich 内容有点混乱,但你可以将 x 的外观更改为类似于 x = factor(ValueName,levels=ValueName[Response=="A1"][order(Percent[Response=="A1"])]) 的内容。 - Andrew Gustar

5

如果预先计算好订单,这并不难:

library(magrittr)
library(dplyr)

o <- d %>% filter(Response == "A1") %>% arrange(Percent) %>% extract2("ValueName")

d %>% 
  mutate(ValueName = factor(ValueName, o)) %>% 
  ggplot() +
  aes(x = ValueName, y = Percent, fill = reorder(Response, plotOrder)) +
  geom_bar(position = "fill", stat = "identity") +
  coord_flip()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接