如何使用 facet_wrap 拆分列并绘制图形?

How to split columns and plot a graph using facet_wrap?

https://www.kaggle.com/shivamb/netflix-shows-and-movies-exploratory-analysis 包含数据集。 (2.13MB)

我正在尝试从 netflix 数据集中拆分国家/地区列,并绘制代表来自三个国家/地区的电影的多面条形图。

可重现的代码如下:-

library(tidyverse)
library(scales)
library(lubridate)

netflix_tbl <- read.csv("netflix_titles_nov_2019.csv")

netflix_wrangled_tbl <- netflix_tbl%>%
    mutate(date_added = dmy(date_added), 
           date = day(date_added), month = month(date_added), year = year(date_added),
           count = readr::parse_number(as.character(duration)),
           show_type = stringr::str_remove(duration, as.character(count)))

netflix_wrangled_tbl %>%
    filter(type == "Movie") %>% 
    separate_rows(country, sep = ",")%>% 
    filter(country == "India" | country == "United States"| country == "United Kingdom")%>%
  separate_rows(cast, sep = ",")%>%
  # Count by country and cast
  count(country, cast)%>%
  slice_max(n, n = 24)%>%
  ggplot(aes(y = tidytext::reorder_within(cast, n, country), x = n))+
  geom_col() +
  tidytext::scale_y_reordered() +
  facet_wrap(~country, scales = "free")

结果输出是,

预期的输出是:-

我可以知道我哪里出错了以及如何实现预期的输出吗?谢谢。

尝试修改代码的最后一个片段:

netflix_wrangled_tbl %>%
  filter(type == "Movie") %>% 
  separate_rows(country, sep = ",")%>% 
  filter(country == "India" | country == "United States"| country == "United Kingdom")%>%
  separate_rows(cast, sep = ",")%>%
  filter(cast!="") %>%
  # Count by country and cast
  count(country, cast)%>%
  group_by(country) %>% arrange(desc(n)) %>%
  group_by(country) %>%
  slice(seq_len(24)) %>%
  ggplot(aes(y = tidytext::reorder_within(cast, n, country), x = n))+
  geom_col() +
  tidytext::scale_y_reordered() +
  facet_wrap(~country, scales = "free")