重新排序单个翻转堆积条
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”在堆叠条的“右边”。
我试过:
- 在dataframe上排列然后plot,不行
x = reorder(var,count)
和 x = reorder(var,-count)
也不起作用
您可以使用 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()
我有以下数据集
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”在堆叠条的“右边”。
我试过:
- 在dataframe上排列然后plot,不行
x = reorder(var,count)
和x = reorder(var,-count)
也不起作用
您可以使用 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()