2 回答

TA贡献1804条经验 获得超2个赞
如果要以相反的顺序堆叠,请尝试 forcats::fct_rev()
library(ggplot2) # version 2.2.1 used
plot_df <- data.frame(group = rep(1:4, 6),
levels = factor(c(rep(1:5, each = 4), rep(1, 4))))
ggplot(plot_df, aes(group, fill = forcats::fct_rev(levels))) +
geom_bar(position = "fill")
反向水平
这是原始情节:
ggplot(plot_df, aes(group, fill = levels)) +
geom_bar(position = "fill")
原始情节
或者,position_fill(reverse = TRUE)按照alistaire在评论中的建议使用:
ggplot(plot_df, aes(group, fill = levels)) +
geom_bar(position = position_fill(reverse = TRUE))
在此处输入图片说明
请注意,图例中的级别(颜色)与堆叠条形的顺序不同。

TA贡献1794条经验 获得超7个赞
position_fill(reverse = TRUE)
标准化条,position_stack(reverse = TRUE)
如果您不想标准化条,请使用。
- 2 回答
- 0 关注
- 835 浏览
添加回答
举报