根据特定级别对堆积的条形图进行排序累积值?

时间:2019-02-08 12:01:59

标签: r ggplot2

给定使用X级别因子作为填充的任何归一化堆叠条形图,是否有任何简单的方法来决定直到对哪个级别的累积值进行排序?

我已经尝试在网上搜索了几个小时,而我却只找到有关如何在一个因素内订购/重新订购水平的答案,我现在绝对知道该怎么做。

我似乎无法根据例如的累积值对多个堆叠图进行排序所需的操作。除了最后一个级别值之外的所有值。

x:离散值,每个值都用一个竖线表示,例如A,B,C,D
y:连续值
f:因子,例如年份水平(2012:2018)

样本数据:

df <- data.frame(f=factor(sample(2012:2018, 500, replace=T)),
                 x=factor(sample(c("A", "B", "C", "D"), 500, replace=T)),
                 y=sample(20:10000, 500, replace=T))

ggplot(df) + geom_bar(aes(x, y, fill = f),
                 position = position_fill(reverse = TRUE), stat = "identity") +
             scale_fill_brewer(palette = "Blues")

取决于值y,堆栈遍布整个位置。一般而言,按y升序/降序排序很容易,但是,有一种方法仅通过累加y值(例如, 2个级别而不是全部4个级别?或例如总结2012年到2017年的所有值,并进行相应排序?

请让我知道是否缺少任何内容,因为这是我的第一篇文章。

1 个答案:

答案 0 :(得分:1)

您要更改相对于累积值的堆叠条形图的顺序,这些顺序是2018年总值的一部分。对吗?

然后,您必须告诉因素x必须如何订购其水平。 ggplot将自动应用此顺序。如您在下图中所看到的,堆叠的条形按第二个堆叠(2013年)的升序排列。

library(dplyr)
library(ggplot2)
df <- data.frame(f=factor(sample(2012:2018, 500, replac e = T)),
                 x=factor(sample(c("A", "B", "C", "D"), 500, replace = T)),
                 y=sample(20:10000, 500, replace = T))

# GET THE DESIRED ORDER
df %>%
  group_by(x, f) %>%
  summarise(Sum = sum(y)) %>%  # sum over years per group
  arrange(f) %>%  # sort by year
  transmute(f, frac = cumsum(Sum) / sum(Sum)) %>%  # get fractions of total value in 2018
  filter(f == 2013) %>%  # get the fractions for the second year (2013)
  arrange(frac) %>%  # order them
  pull(x) -> myOrder  # save vector to order by


df$x <- factor(df$x, levels = myOrder)  # apply ordering

ggplot(df) + geom_bar(aes(x, y, fill = f),
                      position = position_fill(reverse = TRUE), stat = "identity") +
  scale_fill_brewer(palette = "Blues")

enter image description here

相关问题