如何按年份绘制电影类型的细分

时间:2019-05-15 15:36:11

标签: r

我想制作一个堆积面积图,以显示每年发行的电影类型的比例。努力正确地纠缠数据(希望一旦我了解了如何正确格式化数据就可以制作图形)。如何做到这一点,以便按年份掌握每种类型的发行数量?

我的数据框应显示年份,然后x发行的数量是戏剧,y发行的数量是神秘的,依此类推。

为简单起见,我已过滤以查找最常见的流派,但是此处的过滤器的行为不符合预期。

过滤完变量后,我不确定下一步要去哪里。

努力将步骤概念化,但希望这已经足够清楚了... 在此先感谢您的帮助。

library(readr)
library(lubridate)
library(dplyr)

ratings <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-01-08/IMDb_Economist_tv_ratings.csv")
ratings$year_rel <-year(ratings$date)


ratings %>%
  count(genres) %>%
  arrange(desc(n)) %>%
  filter(n > 100)

desired_genres <- c("Comedy, Drama", "Drama", "Action, Crime, Drama", "Action, Adventure, Drama", "Crime", "Drama")

ratings %>%
  select(genres, year_rel) %>%
  filter(genres %in% desired_genres) #this only shows the drama genre

#Unsure where to go from here so as to break down the releases by genre.

1 个答案:

答案 0 :(得分:1)

看来您快到了。您的过滤器行为异常,因为在数据中,类型列没有空格,而您的desired_genres向量也有空格。与其重新输入外观,不如通过在流水线末端使用pull(genres)并以编程方式提取所需类型来避免错误,并将结果分配给desired_genres向量。

然后添加一个group_by和一个add_count

library(readr)
library(lubridate)
library(dplyr)

ratings <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-01-08/IMDb_Economist_tv_ratings.csv")
ratings$year_rel <-year(ratings$date)


desired_genres <- ratings %>%
  count(genres) %>%
  arrange(desc(n)) %>%
  filter(n > 100) %>%
  pull(genres)

filtered_rating <- ratings %>%
  select(genres, year_rel) %>%
  filter(genres %in% desired_genres) %>%
  group_by(year_rel) %>%
  add_count(genres)