按填充子集顺序排列堆积条中的x轴 [英] Order x axis in stacked bar by subset of fill
问题描述
关于如何安排有多个问题(例如,此处)在带有 ggplot2
的条形图中按频率显示x轴.但是,我的目的是通过填充子集的相对频率在堆叠条形图中的X轴上排列类别.例如,我想按变量 z
中类别 B
的百分比对x轴进行排序.
There are multiple questions (here for instance) on how to arrange the x axis by frequency in a bar chart with ggplot2
. However, my aim is to arrange the categories on the X-axis in a stacked bar chart by the relative frequency of a subset of the fill. For instance, I would like to sort the x-axis by the percentage of category B
in variable z
.
这是我第一次尝试仅使用ggplot2
This was my first try using only ggplot2
library(ggplot2)
library(tibble)
library(scales)
factor1 <- as.factor(c("ABC", "CDA", "XYZ", "YRO"))
factor2 <- as.factor(c("A", "B"))
set.seed(43)
data <- tibble(x = sample(factor1, 1000, replace = TRUE),
z = sample(factor2, 1000, replace = TRUE))
ggplot(data = data, aes(x = x, fill = z, order = z)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent)
如果这不起作用,我使用dplyr创建了一个汇总数据框,然后分发数据并按 B
对其进行排序,然后再次进行收集.但是,绘图也不起作用.
When that didn't work I created a summarised data frame using dplyr and then spread the data and sort it by B
and then gather it again. But plotting that didn't work either.
library(dplyr)
library(tidyr)
data %>%
group_by(x, z) %>%
count() %>%
spread(z, n) %>%
arrange(-B) %>%
gather(z, n, -x) %>%
ggplot(aes(x = reorder(x, n), y = n, fill = z)) +
geom_bar(stat = "identity", position = "fill") +
scale_y_continuous(labels = percent)
我更喜欢仅使用ggplot的解决方案,以便不依赖于dplyr/tidyr创建的数据帧中的顺序.但是,我愿意接受任何东西.
I would prefer a solution with ggplot only in order not to be dependent of the order in the data frame created by dplyr/tidyr. However, I'm open for anything.
推荐答案
如果要按绝对频率排序:
If you want to sort by absolute frequency:
lvls <- names(sort(table(data[data$z == "B", "x"])))
如果要按相对频率排序:
If you want to sort by relative frequency:
lvls <- names(sort(tapply(data$z == "B", data$x, mean)))
然后您可以在 ggplot
内动态创建因子:
Then you can create the factor on the fly inside ggplot
:
ggplot(data = data, aes(factor(x, levels = lvls), fill = z)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = percent)
这篇关于按填充子集顺序排列堆积条中的x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!