如何将多个列组合在一起并绘制条形图?

How to group multiple columns together and plot a bar graph?

我有一个使用 splitstackshape 包拆分的数据框。拆分后,我无法继续对多列进行分组并绘制条形图。代码如下,

library(tidyverse)
library(splitstackshape)
df <- data.frame(countries=(c("England","Australia,Pakistan", "India,England","Denmark", "",
                             "Australia, Pakistan, New Zealand, England", "United States, England,Pakistan")))
data_split <- splitstackshape::cSplit(df, "countries", ",")
data_split

输出结果如下,

countries_1       countries_2 countries_3     countries_4
1:       England        <NA>        <NA>        <NA>
2:     Australia    Pakistan        <NA>        <NA>
3:         India     England        <NA>        <NA>
4:       Denmark        <NA>        <NA>        <NA>
5:          <NA>        <NA>        <NA>        <NA>
6:     Australia    Pakistan New Zealand     England
7: United States     England    Pakistan        <NA>

根据以上输出,我希望绘制一个条形图,其中包含按降序排列的国家/地区频率。示例输出如下, bar chart showing frequency of countries in descending order

像这样:

library(tidyverse)
library(ggplot2)

df %>% 
  separate_rows(countries, sep = ",") %>% 
  count(countries) %>% 
  ggplot(aes(y = fct_reorder(countries, n), x = n)) +
  geom_col()


根据评论编辑:仅绘制 10 个最常见的国家/地区:

df %>% 
  separate_rows(countries, sep = ",") %>% 
  count(countries) %>% 
  slice_max(n, n = 10) %>% 
  ggplot(aes(y = fct_reorder(countries, n), x = n)) +
  geom_col()