重新排序单个翻转堆积条

Reordering Single Flipped Stacked Bar

我有以下数据集

example <- structure(list(selection_type = c("P", "G"), count = c(44L, 
102L), var = c("ST", "ST")), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"))

我制作了以下情节:

ggplot(example) +
  geom_bar( aes(x = var, y = count, fill = selection_type), stat = "identity") +
  coord_flip()


问题:我需要倒退。所以“P”在堆叠条的“右边”。

我试过:

您可以使用 forcats 包中的 fct_rev

library(tidyverse)
example <- structure(list(selection_type = c("P", "G"), count = c(44L, 
                                                                  102L), var = c("ST", "ST")), row.names = c(NA, 
                                                                                                             -2L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(example) +
  geom_bar( aes(x = var, y = count, fill = fct_rev(selection_type)), stat = "identity") +
  coord_flip()