library(readr)
netflix_titles <- read_csv("C:/Users/Ar programing/Downloads/netflix_titles.csv/netflix_titles.csv")
## New names:
## Rows: 8809 Columns: 26
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (11): show_id, type, title, director, cast, country, date_added, rating,... dbl
## (1): release_year lgl (14): ...13, ...14, ...15, ...16, ...17, ...18, ...19,
## ...20, ...21, ......
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...13`
## • `` -> `...14`
## • `` -> `...15`
## • `` -> `...16`
## • `` -> `...17`
## • `` -> `...18`
## • `` -> `...19`
## • `` -> `...20`
## • `` -> `...21`
## • `` -> `...22`
## • `` -> `...23`
## • `` -> `...24`
## • `` -> `...25`
## • `` -> `...26`
 #How many movies vs. TV shows are available on Netflix?
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(plotly) 
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
netflix_sort<- group_by(netflix_titles,type) %>% arrange(type)
NO_OF_movies_vs_TV_shows <- netflix_sort %>% count(type)
graph.Number_of_Movies_vs_TV_Shows_on_Netflix<-ggplot(NO_OF_movies_vs_TV_shows, aes(x = type, y = n)) +
  geom_bar(stat = "identity", fill = "skyblue") +  
  labs(x = "Type", y = "Count", title = "Number of Movies vs. TV Shows on Netflix")
  ggplotly(graph.Number_of_Movies_vs_TV_Shows_on_Netflix)
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly) 
# Which countries produce the most content available on Netflix?
netflix_sort_country<- netflix_titles %>% group_by(country) %>% arrange(country)

no_of_show_by_each_country <- netflix_sort_country %>% count(country)

ggplot(no_of_show_by_each_country, aes(x = country, y = n)) +
  geom_bar(fill = "skyblue", stat = "identity") +
  labs(x = "Country", y = "Number of Shows", title = "Number of Netflix Shows by Country")

library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly) 

# What are the most popular genres on Netflix?
netflix_genres <- netflix_titles %>% select(title, listed_in) %>% separate_rows(listed_in, sep = ", ") %>% mutate(genre = trimws(listed_in)) %>% select(-listed_in)

genre_counts <- netflix_genres %>% group_by(genre) %>% summarise(count = n()) %>% arrange(desc(count))

top_10_genres <- head(genre_counts, 10)

top_10_genres
## # A tibble: 10 × 2
##    genre                    count
##    <chr>                    <int>
##  1 International Movies      2752
##  2 Dramas                    2427
##  3 Comedies                  1674
##  4 International TV Shows    1351
##  5 Documentaries              869
##  6 Action & Adventure         859
##  7 TV Dramas                  763
##  8 Independent Movies         756
##  9 Children & Family Movies   641
## 10 Romantic Movies            616
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly) 
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ plotly::filter() masks dplyr::filter(), stats::filter()
## ✖ dplyr::lag()     masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)

# How has the distribution of content (Movies vs. TV shows) changed over time?

netflix_titles$date_added <- as.Date(netflix_titles$date_added, format = "%B %d, %Y")

netflix_titles$year_added <- year(netflix_titles$date_added)

netflix_titles$month_added <- month(netflix_titles$date_added)

content_distribution <- netflix_titles %>%
  mutate(type = ifelse(type == "TV Show", "TV Show", "Movie")) %>%  group_by(year_added, type) %>% summarise(count = n()) %>% 
  spread(key = type, value = count, fill = 0) %>% ungroup() %>% arrange(year_added)
## `summarise()` has grouped output by 'year_added'. You can override using the
## `.groups` argument.
graph2<-ggplot(content_distribution, aes(x = year_added)) +
  geom_line(aes(y = `Movie`, color = "Movies")) + 
  geom_line(aes(y = `TV Show`, color = "TV Shows")) +
  scale_color_manual(values = c("blue", "red")) +
  labs(title = "Distribution of Movies and TV Shows Over Time",x = "Year",y = "Count",color = "Type") +  
  theme_minimal()

ggplotly(graph2)
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly) 

# How has genre popularity evolved over the years?
netflix_genres <- netflix_titles %>%
  select(title, listed_in, release_year) %>%
  separate_rows(listed_in, sep = ", ") %>%
  mutate(genre = trimws(listed_in))

genre_counts <- netflix_genres %>%
  group_by(release_year, genre) %>%
  summarise(count = n()) %>%
  ungroup()
## `summarise()` has grouped output by 'release_year'. You can override using the
## `.groups` argument.
graph3<-ggplot(genre_counts, aes(x = release_year, y = count, color = genre)) +geom_line() +
  scale_x_continuous(breaks = seq(min(genre_counts$release_year), max(genre_counts$release_year), by = 5)) +
  labs(title = "Evolution of Genre Popularity on Netflix Over the Years",x = "Release Year",y = "Count",color = "Genre") +
  theme_minimal() +theme(legend.position = "right") +
  scale_color_viridis_d()

ggplotly(graph3)
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly) 

# Are there any correlations between the country of production and the genres produced?

country_genre_table <- table(netflix_titles$country, netflix_titles$listed_in)

correlation <- cor(country_genre_table)
## Warning in cor(country_genre_table): the standard deviation is zero
heatmap(correlation, 
        Rowv = NA, Colv = NA,
        col = colorRampPalette(c("blue", "white", "red"))(100),
        margins = c(5, 10),
        main = "Correlation Heatmap: Country and Genre")

library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly) 

# What are the most common age ratings for Netflix content?
rating_counts <- netflix_titles %>%
  group_by(rating) %>%
  summarise(count = n()) %>%
  arrange(desc(count))
rating_counts
## # A tibble: 19 × 2
##    rating   count
##    <chr>    <int>
##  1 TV-MA     3208
##  2 TV-14     2160
##  3 TV-PG      863
##  4 R          799
##  5 PG-13      490
##  6 TV-Y7      334
##  7 TV-Y       307
##  8 PG         287
##  9 TV-G       220
## 10 NR          80
## 11 G           41
## 12 TV-Y7-FV     6
## 13 <NA>         4
## 14 NC-17        3
## 15 UR           3
## 16 66 min       1
## 17 74 min       1
## 18 84 min       1
## 19 A            1
graph4<-ggplot(rating_counts, aes(x = rating, y = count)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(title = "Most Common Age Ratings for Netflix Content",
       x = "Age Rating",
       y = "Count") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

ggplotly(graph4)
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)

# Is there a relationship between content type (movie/TV show) and age rating?
netflix_content_rating <- netflix_titles %>%
  select(type, rating) %>%
  filter(!is.na(type), !is.na(rating))

content_rating_cross_tab <- table(netflix_content_rating$type,netflix_content_rating$rating)

content_rating_df <- as.table(content_rating_cross_tab)

correlation_matrix <- cor(content_rating_df)

heatmap(correlation_matrix, 
        Rowv = NA, Colv = NA,
        col = colorRampPalette(c("blue", "white", "red"))(100),
        margins = c(5, 10),
        main = "Correlation Heatmap: Content Type and Age Rating")

library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)

# What is the average number of seasons for TV shows?
tv_shows <- netflix_titles %>%
  filter(type == "TV Show")

tv_shows$seasons <- as.numeric(gsub("[^0-9]", "", tv_shows$duration))

round(mean(tv_shows$seasons, na.rm = TRUE))
## [1] 2
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)

# Are there trends in content duration over time?

average_duration <- netflix_titles %>%
  mutate(release_year = as.numeric(str_sub(release_year, 1, 4))) %>%
  mutate(duration = as.numeric(gsub("[^0-9]", "", duration))) %>%
  group_by(type, release_year) %>%
  summarise(average_duration = mean(duration, na.rm = TRUE))
## `summarise()` has grouped output by 'type'. You can override using the
## `.groups` argument.
graph5 <- ggplot(average_duration, aes(x = release_year, y = average_duration, color = type)) +
  geom_line() +
  labs(title = "Average Duration of Netflix Content Over Time",
       x = "Release Year",
       y = "Average Duration (minutes)",
       color = "Type") +
  theme_minimal()

ggplotly(graph5)
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)

# Who are the most prolific directors on Netflix?
directors <- netflix_titles %>%
  filter(!is.na(director)) %>%
  mutate(director = strsplit(director, ", ")) %>%
  unnest(director)

director_counts <- directors %>%
  group_by(director) %>%
  summarise(number_of_titles = n()) %>%
  arrange(desc(number_of_titles))
top_directors <- head(director_counts, 10)
top_directors
## # A tibble: 10 × 2
##    director            number_of_titles
##    <chr>                          <int>
##  1 Rajiv Chilaka                     22
##  2 Jan Suter                         21
##  3 Raúl Campos                       19
##  4 Marcus Raboy                      16
##  5 Suhas Kadav                       16
##  6 Jay Karas                         15
##  7 Cathy Garcia-Molina               13
##  8 Jay Chapman                       12
##  9 Martin Scorsese                   12
## 10 Youssef Chahine                   12
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)


# Which actors/actresses appear most frequently in Netflix content?
actors <- netflix_titles %>%
  filter(!is.na(cast)) %>%
  mutate(cast = strsplit(cast, ", ")) %>%
  unnest(cast)
## Warning: There were 2 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `cast = strsplit(cast, ", ")`.
## Caused by warning in `strsplit()`:
## ! unable to translate 'Bradley Cooper, Jennifer Lawrence, Toby Jones, Rhys Ifans, Ana Ularu, Sam Reid, Sean Harris, Blake Ritson, David Dencik, Charity Wakefield, Ned Dennehy, Kim Bodnia, Christian McKay, Conleth Hill, Bodil J<f8>rgensen, Douglas Hodge, Hilton McRae, Pavel K...' to a wide string
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
actor_counts <- actors %>%
  group_by(cast) %>%
  summarise(appearances = n()) %>%
  arrange(desc(appearances))
head(actor_counts, 10)
## # A tibble: 10 × 2
##    cast             appearances
##    <chr>                  <int>
##  1 Anupam Kher               43
##  2 Shah Rukh Khan            35
##  3 Julie Tejwani             33
##  4 Naseeruddin Shah          32
##  5 Takahiro Sakurai          32
##  6 Rupa Bhimani              31
##  7 Akshay Kumar              30
##  8 Om Puri                   30
##  9 Yuki Kaji                 29
## 10 Amitabh Bachchan          28