我正在用ggplot创build一个堆积条形图,如下所示: plot_df <- df[!is.na(df$levels), ] ggplot(plot_df, aes(group)) + geom_bar(aes(fill = levels), position = "fill") 这给了我这样的东西: 我如何反转堆叠的酒吧本身的顺序,使1级在底部,5级在每个酒吧的顶部? 我已经看到了这方面的一些问题(例如, 如何控制使用ggplot2上的标识的堆叠条形图的sorting ),而常见的解决scheme似乎是按照该级别对数据框进行重新sorting,因为ggplot正在使用确定顺序 所以我尝试使用dplyr重新sorting: plot_df <- df[!is.na(df$levels), ] %>% arrange(desc(levels)) 不过,情节也是一样的。 无论我按升序还是降序排列,似乎都没有什么差别 这是一个可重现的例子: group <- c(1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4) levels <- c("1","1","1","1","2","2","2","2","3","3","3","3","4","4","4","4","5","5","5","5","1","1","1","1") plot_df <- data.frame(group, levels) ggplot(plot_df, aes(group)) + geom_bar(aes(fill = levels), position = "fill")