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

时间:2020-07-25 14:40:38

标签: r ggplot2 facet

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")

结果输出为

enter image description here

预期输出为:-

enter image description here

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

1 个答案:

答案 0 :(得分:2)

尝试使用以下命令修改代码的最后一部分:

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")

enter image description here