通过变量之一的值设置堆积条形图的顺序 [英] Set the order of a stacked bar chart by the value of one of the variables

查看:74
本文介绍了通过变量之一的值设置堆积条形图的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求制作一个堆叠的条形图,其中的条形和值以精确的方式堆叠和排序.

I've been asked to produce a stacked bar chart with bars and values that are stacked and ordered in a precise manner.

在这种情况下,左侧为"A3",中间为"A2",右侧为"A1".我已经覆盖了.

In this case "A3" on the left, "A2" in the middle and "A1" on the right. I've got that covered.

让我不知道的一点是,我还被要求按"A1"的值对小节的降序进行排序.在这种情况下,这意味着值11"出现在顶部,然后降序为值6".条形的顺序记录在向量"plotOrder"中,我在下面的当前尝试中尝试使用它来实现某些目的.

The bit that escapes me is that I've also been asked to order the bars descending order by the value of "A1". In this case that would mean 'Value 11' appearing at the top, descending in order to 'Value 6'. The ordering of the bars is recorded in the vector 'plotOrder', which I've tried to use to achieve something in my current attempt below.

在拖曳SO上有关订购堆叠条形图的许多问题时,我没有找到描述或解决此特定问题的问题或答案.任何帮助将不胜感激.

In trawling the many questions posed on SO about ordering stacked bar charts I have not found a question or an answer that describes or solves this particular problem. Any assistance would be very much appreciated.

下面的简化代码下载了一些随机数据,这些数据与我目前使用的格式相同,并生成了下面的图表.

The simplified code below downloads some random data that is in the same form with which I'm currently working with and produces the chart below.

library(ggplot2)

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

哪个会生成此图表:

数据:

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

在此先感谢您提供的任何建议或解决方案.

Thank you in advance for any advice or solutions you might have.

推荐答案

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

You already have plotOrder, but you need to apply it to reorder x rather than fill...

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

这篇关于通过变量之一的值设置堆积条形图的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆