如何在ggplot2中通过组facet_wrap对分类变量的频率计数进行排序?

inkz8wg9  于 2023-05-11  发布在  其他
关注(0)|答案(2)|浏览(171)

我试图创建按组内频率排序的分类数据的条形图。例如,使用钻石数据集,我希望条形图按“切割”分组的“清晰度”值的计数/频率排序。

data(diamonds)
library(tidyverse)

diamonds$cut <- as.character(diamonds$cut)
diamonds$clarity <- as.character(diamonds$clarity)

diamonds %>% 
  group_by(cut) %>% 
  count(clarity, order = FALSE) %>% 
  mutate(clarity = as.factor(reorder(clarity, n))) %>% 
  ggplot(aes(n, clarity, fill = cut)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~ cut, ncol = 2, scales = "free") +
  labs(x = "count", y = NULL) +
  theme_bw()

这将生成以下图形:

这是非常接近我想要的,但唯一的图表实际上是由清晰度频率排序的第一,“公平”。我如何使所有的条形图清晰度频率排序?
先谢谢你了。

jum4pzuy

jum4pzuy1#

从概念上讲,如果你在每个图上有不同顺序的y轴,那么我认为你有五个不同的图,而不是一个单一的多面图。因此,您可以将其绘制为:

plots <- lapply(levels(factor(diamonds$cut)), function(x) {
  diamonds %>% 
  group_by(cut) %>% 
  count(clarity, order = FALSE) %>% 
  filter(cut == x) %>%
  mutate(clarity = as.factor(reorder(clarity, n))) %>% 
  ggplot(aes(n, clarity)) +
  geom_col(fill = scales::hue_pal()(5)[match(x, levels(factor(diamonds$cut)))])+
  labs(title = x, x = "count", y = NULL) +
  theme_bw()
  }
)

library(patchwork)

  (plots[[1]] + plots[[2]]) / 
  (plots[[3]] + plots[[4]]) / 
  (plots[[5]] + plot_spacer())

xqnpmsa8

xqnpmsa82#

一种选择是使用tidytext包中的reorder_withinscale_y_reorderd

library(ggplot2)
library(dplyr, warn = FALSE)
library(tidytext)

diamonds$cut <- as.character(diamonds$cut)
diamonds$clarity <- as.character(diamonds$clarity)

diamonds %>%
  group_by(cut) %>%
  count(clarity, order = FALSE) %>%
  mutate(clarity = tidytext::reorder_within(clarity, n, cut)) %>%
  ggplot(aes(n, clarity, fill = cut)) +
  geom_col(show.legend = FALSE) +
  tidytext::scale_y_reordered() +
  facet_wrap(~cut, ncol = 2, scales = "free") +
  labs(x = "count", y = NULL) +
  theme_bw()

相关问题