我有一些数据,希望按一组的最高平均值进行排序,然后将它们绘制为堆积条形图。我已经通过生成几个数据帧来做到这一点,但这是冗长的,我想知道是否有一种不太冗长的方法?
set.seed(3)
x <- rep(letters[1:5], 3)
fill <- rep(letters[24:26], 5)
n <- runif(15, 0, 1)
df <- data.frame(x, fill, n)
df2 <- df %>%
group_by(x) %>%
mutate(percent = n/sum(n))
df3 <- df2 %>%
group_by(fill) %>%
summarise(mean = mean(percent)) %>%
ungroup() %>%
arrange(desc(mean))
df3 <- df2[df2$fill == df3$fill[1], ] %>%
arrange(desc(percent))
df$x <- factor(df$x, levels = df3$x)
ggplot(data = df, aes(x, y, fill = fill)) +
geom_col(position = position_fill())
答案 0 :(得分:1)
我不确定这是否一定更好,但这是一种在您的问题中产生相同图形的方法:
df %>%
group_by(x) %>%
mutate(pct = n / sum(n)) %>%
ungroup() %>%
arrange(fill != "z", desc(pct)) %>%
group_by(fill) %>%
mutate(order = row_number()) %>%
ggplot(aes(fct_reorder(x, order), pct, fill = fill)) +
geom_col()
根据您实际尝试比较的内容,您可能需要考虑其他顺序或方面。例如,考虑选择多面还是堆叠时会发生什么:
df %>%
group_by(x) %>%
mutate(pct = n / sum(n)) %>%
ggplot(aes(x, pct, fill = fill)) +
geom_col() +
facet_wrap(~ fill)
更新2019-02-18(每条评论)
更新以提前抽象掉知道的z
。对于每个mean(pct)
,按fill
排序,后跟pct
:
df %>%
group_by(x) %>%
mutate(pct = n / sum(n)) %>%
group_by(fill) %>%
mutate(mean_pct = mean(pct)) %>%
arrange(desc(mean_pct), desc(pct)) %>%
mutate(order = row_number()) %>%
ggplot(aes(fct_reorder(x, order), pct, fill = fill)) +
geom_col()