Created
April 21, 2021 18:34
-
-
Save beatrizmilz/d192a861f77dd937dcf5c2bd66b87a60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Author: Bea | |
#' Subject: Tidytuesday de 20 de abril de 2021 | |
library(magrittr, include.only = "%>%") | |
library(ggplot2) | |
# Import ---------------------------------------------------------------------- | |
netflix <- | |
readr::read_csv( | |
'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-04-20/netflix_titles.csv' | |
) | |
# Tidy ------------------------------------------------------------------------- | |
netflix %>% dplyr::glimpse() | |
# Rows: 7,787 | |
# Columns: 12 | |
# $ show_id <chr> "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9"… | |
# $ type <chr> "TV Show", "Movie", "Movie", "Movie", "Movie", "TV S… | |
# $ title <chr> "3%", "7:19", "23:59", "9", "21", "46", "122", "187"… | |
# $ director <chr> NA, "Jorge Michel Grau", "Gilbert Chan", "Shane Acke… | |
# $ cast <chr> "João Miguel, Bianca Comparato, Michel Gomes, Rodolf… | |
# $ country <chr> "Brazil", "Mexico", "Singapore", "United States", "U… | |
# $ date_added <chr> "August 14, 2020", "December 23, 2016", "December 20… | |
# $ release_year <dbl> 2020, 2016, 2011, 2009, 2008, 2016, 2019, 1997, 2019… | |
# $ rating <chr> "TV-MA", "TV-MA", "R", "PG-13", "PG-13", "TV-MA", "T… | |
# $ duration <chr> "4 Seasons", "93 min", "78 min", "80 min", "123 min"… | |
# $ listed_in <chr> "International TV Shows, TV Dramas, TV Sci-Fi & Fant… | |
# $ description <chr> "In a future where the elite inhabit an island parad… | |
netflix_clean <- netflix %>% | |
# Transformar em data: date_added | |
dplyr::mutate(date_added = readr::parse_date(date_added, format = "%B %d, %Y")) %>% | |
# Separar a coluna listed_in - 3 categorias | |
tidyr::separate( | |
listed_in, | |
sep = ",", | |
into = paste0("listed_in_", rep(1:3)), | |
fill = "right" | |
) %>% | |
# Transformar em número: duration. Separei em 2 variáveis: | |
# duration_seasons para séries | |
# duration_minutes para filmes | |
dplyr::mutate( | |
duration_seasons = dplyr::case_when(type == "TV Show" ~ readr::parse_number(duration)), | |
duration_minutes = dplyr::case_when(type == "Movie" ~ readr::parse_number(duration)) | |
) %>% | |
# Altera a ordem da base, deixa todos os duration juntos | |
dplyr::relocate(duration_seasons, duration_minutes, .after = duration) %>% | |
# Remove espaços desnecessários criados ao separar o conteúdo de listed_in | |
dplyr::mutate(dplyr::across(tidyselect::starts_with("listed_in_"), stringr::str_trim)) | |
# Visualize -------------------------------------------------------------------- | |
## Filme ou série? O que tem mais no catálogo?--------- | |
netflix_clean %>% | |
# dplyr::group_by(type) %>% | |
ggplot() + | |
geom_bar(aes(x = type), fill = "#e50815", alpha = 0.8) + | |
theme_bw() + | |
labs(x = "Tipo", y = "Quantidade de itens") | |
# Catálogo da netflix tem muito mais filmes do que séries! | |
## Quantas temporadas as séries costumam durar? ------------- | |
netflix_clean %>% | |
dplyr::filter(type == "TV Show") %>% | |
ggplot() + | |
geom_bar(aes(x = duration_seasons), fill = "#e50815", alpha = 0.8) + | |
theme_bw() + | |
labs(x = "Número de temporadas", y = "Quantidade de séries") + | |
scale_x_continuous(breaks = 1:15) | |
# A maioria das séries da netflix tem 1 temporada. | |
# 75% das séries possui até 2 temporadas. | |
# a série mais longa é Grays Anatomy, com 16 temporadas! | |
## Quanto tempo os filmes costumam durar? ----- | |
netflix_clean %>% | |
dplyr::filter(type == "Movie") %>% | |
ggplot() + | |
geom_density( | |
aes(x = duration_minutes), | |
fill = "#e50815", | |
alpha = 0.8, | |
bins = 100 | |
) + | |
theme_bw() + | |
labs(x = "Minutos", y = "Quantidade de Filmes") + | |
scale_x_continuous(breaks = seq(from = 0, to = 360, by = 30)) | |
# A maioria dos filmes tem entre ~1h20 e 2h | |
# o "filme" mais longo é na verdade um episódio de série: Black Mirror: Bandersnatch | |
## Em qual ano o conteúdo foi produzido? ------- | |
netflix_clean %>% | |
dplyr::count(release_year) %>% | |
ggplot() + | |
geom_col(aes(x = release_year, y = n), fill = "#e50815", | |
alpha = 0.8) + | |
theme_bw() + | |
labs(x = "Ano de lançamento", y = "Quantidade de Filmes/Séries") | |
# Os anos em que foi lançado a maioria dos itens da Netflix são | |
# entre 2016 e 2020. O catálogo é bem atual! | |
## Em qual ano o item foi adicionado no catálogo? ------ | |
netflix_clean %>% | |
dplyr::mutate(mes_ano = lubridate::floor_date(date_added, unit = "year")) %>% | |
dplyr::count(mes_ano) %>% | |
ggplot() + | |
geom_col(aes(x = mes_ano, y = n), fill = "#e50815", | |
alpha = 0.8) + | |
theme_bw() + | |
labs(x = "Ano de adição ao catálogo", y = "Quantidade de Filmes/Séries") + | |
scale_x_date(breaks = "2 years", date_labels = "%Y") | |
# A netflix começou a adicionar itens no catálogo em 2008! | |
# O ano em que a netflix mais adicionou conteúdo foi 2019. | |
## Quais são as categorias em que a Netflix mais investe? -------- | |
netflix_clean %>% | |
tidyr::pivot_longer( | |
cols = c("listed_in_1", "listed_in_2", "listed_in_3"), | |
values_to = "listed_in" | |
) %>% | |
dplyr::mutate( | |
listed_in = stringr::str_replace_all(listed_in, "TV Shows|Movies|TV|'", ""), | |
listed_in = stringr::str_trim(listed_in), | |
listed_in = dplyr::case_when( | |
listed_in %in% c("Anime Features", "Anime Series") ~ "Anime", | |
listed_in %in% c("Children & Family", "Kids") ~ "Children & Family | Kids", | |
listed_in %in% c("Stand-Up Comedy", "Stand-Up Comedy & Talk Shows") ~ "Stand-Up Comedy & Talk Shows", | |
TRUE ~ listed_in | |
) | |
) %>% | |
dplyr::count(listed_in) %>% | |
tidyr::drop_na(listed_in) %>% | |
dplyr::filter(listed_in != "") %>% | |
dplyr::mutate( | |
listed_in = forcats::fct_lump(listed_in, 10, w = n), | |
listed_in = forcats::fct_reorder(listed_in, n) | |
) %>% | |
ggplot() + | |
geom_col(aes(y = listed_in, x = n), fill = "#e50815", alpha = 0.8) + | |
theme_bw() + | |
labs(y = "Categoria", x = "Quantidade de ocorrência") | |
# Netflix investe bastante no catálogo internacional | |
# Principais categorias (fora internacional): Dramas, comédias, infantil | |
# Quais são os principais países em que são produzidos conteúdos? | |
netflix_clean %>% | |
# Separar a coluna country | |
tidyr::separate( | |
country, | |
sep = ",", | |
into = paste0("country_", rep(1:15)), | |
fill = "right", | |
remove = FALSE | |
) %>% | |
dplyr::mutate(dplyr::across(tidyselect::starts_with("country_"), stringr::str_trim)) %>% | |
tidyr::pivot_longer( | |
cols = tidyselect::starts_with("country_"), | |
values_drop_na = TRUE, | |
names_to = "order_country", | |
values_to = "countries" | |
) %>% | |
dplyr::count(countries) %>% View() | |
dplyr::mutate( | |
countries = forcats::fct_lump(countries, 10, w = n), | |
countries = forcats::fct_reorder(countries, n) | |
) %>% | |
ggplot() + | |
geom_col(aes(y = countries, x = n), fill = "#e50815", alpha = 0.8) + | |
theme_bw() + | |
labs(y = "País", x = "Quantidade de ocorrência") | |
# O principal (de muito longe) país que produz conteúdos pra netflix é os EUA, | |
# seguido pela India (com muita diferença). | |
# Brasil não está no TOP 10! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment