Explore your activity on Netflix with R: How to analyze and visualize your viewing history | by Saúl Buentello | Towards Data Science

The arrival of video streaming services revolutionized the entertainment industry and with it, many of our habits also changed. Whether you spend hours or minutes watching a movie or TV series, Netflix has become a favorite on the scene.
Netflix, like many applications today, allows its users to be free to view their viewing activity history. For this article, we will use my history to analyze and visualize some interesting data.
As an active Netflix user and with access to account data, you must enter the URL https://www.netflix.com/viewingactivity, where you will find a list of the titles that you have recently seen on the platform.
Before the end of the page, there is a link that says “Download it all”, which by clicking will invite you to download a CSV file with your complete history. This file by default is downloaded with the name “NetflixViewingHistory.csv”, and is what we need to work on R.
Well, creating a new R script, you can import the packages that we will be using and the downloaded CSV data to start analyzing them, with a more readable date format.
# LIBRARIESlibrary(dplyr)library(tidyr)library(lubridate)library(zoo)library(ggplot2)# READING DATA FROM CSV DOWNLOADED FROM NETFLIX ACCOUNTminetflix <- read.csv("NetflixViewingHistory.csv") str(minetflix)minetflix$Date <- dmy(minetflix$Date)
As you can see, the CSV file only has two columns: Date (the exact date) and Title (the title of the movie or episode and season of the TV series you have seen).
Many of us get hooked watching one or more TV series, and suddenly you have spent hours of being in an almost hypnotic state in which the rest simply does not matter. There is no definition of how many hours or how many episodes “Binge-watching” is, but we are all familiar with the concept. Making the first analysis, consider that “Binge-watching” is from 6 episodes to more, and let’s see what are the most viewed TV series.
Let’s separate the Title column from our data, taking advantage of the constant in the format in which Netflix names them by TV series title, TV series season, and TV series episode. Let’s also clean the data, removing everything that does not have this format (that is, movies) to focus on TV series. We also need to identify a record of episodes watched per day, belonging to a single TV series.
# SEPARATE TITLE COLUMN IN TITLE OF TV SERIES, SEASON AND EPISODE TITLEminetflix_serie <- minetflix %>%  separate(col = Title, into = c("title", "temporada", "titulo_episodio"), sep = ': ')# REMOVE OCCURRENCES WHERE SEASON AND EPISODE ARE EMPTY (BECAUSE THEY ARE NOT TV SERIES)minetflix_serie <- minetflix_serie[!is.na(minetflix_serie$temporada),]minetflix_serie <- minetflix_serie[!is.na(minetflix_serie$titulo_episodio),]# REGISTRO DE NÚMERO DE EPISODIOS VISTOS POR DÍA, POR SERIEmaratones_minetflix <- minetflix_serie %>%  count(title, Date)# LET'S CONSIDER "BINGE-WATCHING" 6 OR MORE EPISODES PER DAY AND SORT BY DATEmaratones_minetflix <- maratones_minetflix[maratones_minetflix$n >= 6,]maratones_minetflixmaratones_minetflix <- maratones_minetflix[order(maratones_minetflix$Date),]maratones_minetflix
Under those conditions, in my case, I got 35 observations with 3 variables now.
Now we are ready to view the top 10 of the most viewed TV series to which you have dedicated “Binge-watching”. We will group the data by the title of the TV series and we will order them by the number of episodes watched.
# GROUPING DATA BY TV SERIES TITLE AND SORTING BY NUMBER OF EPISODES VIEWEDmaratones_minetflix_todas <- maratones_minetflix %>%   group_by(title) %>%   summarise(n = sum(n)) %>%  arrange(desc(n))# PLOTTING TOP 10 OF BINGE-WATCHING TV SERIESmaratones_minetflix_top <- maratones_minetflix_todas %>%   top_n(10) %>%  ggplot(aes(x = reorder(title, n), y = n)) +  geom_col(fill = "#0097d6") +  coord_flip() +  ggtitle("Top 10 de series más vistas en maratón en mi Netflix", "4 o más episodios por día") +  labs(x = "Serie en Netflix", y = "Episodios vistos en total") +  theme_minimal()maratones_minetflix_top
We will then obtain the following plot. Please don’t judge me for spending so much time watching “Betty, la fea” (It’s a popular Colombian “soap opera”).
It would be interesting to see how much your consuming habits have changed over time. Do the maximum peaks match the lockdown by the Covid-19, for example? Or maybe it coincides with all that time since you were single again?
You can make a general count of episodes per day in the history of your activity.
# EPISODES PER DAYnetflix_episodios_dia <- minetflix %>%  count(Date) %>%  arrange(desc(n))# PLOTTING EPISODES PER DAYnetflix_episodios_dia_plot <- ggplot(aes(x = Date, y = n, color = n), data = netflix_episodios_dia) +  geom_col(color = c("#f16727")) +  theme_minimal() +  ggtitle("Episodios vistos en mi Netflix por día", "Historial de 2016 a 2020") +  labs(x = "Fecha", y = "Episodios vistos") netflix_episodios_dia_plot
In my case, in the following plot, it’s very remarkable that in my Netflix account consumption has been increasing.
You can go deeper. Let’s look at the activity in another way, with a heatmap distributed by days, weeks, months, and years, with which we can specify a little better the times when we had more or less consumption of episodes of TV series per day.
# CALENDAR WITH NUMBER OF EPISODES SEEN PER DAY IN HEATMAPnetflix_episodios_dia <- netflix_episodios_dia[order(netflix_episodios_dia$Date),]netflix_episodios_dia$diasemana <- wday(netflix_episodios_dia$Date)netflix_episodios_dia$diasemanaF <- weekdays(netflix_episodios_dia$Date, abbreviate = T)netflix_episodios_dia$mesF <- months(netflix_episodios_dia$Date, abbreviate = T)# YOU DON'T NEED TO RENAME NECESSARILY IN SPANISH DAYS OF THE WEEK AND MONTHSnetflix_episodios_dia$diasemanaF <-factor(netflix_episodios_dia$diasemana, levels = rev(1:7), labels = rev(c("Lun","Mar","Mier","Jue","Vier","Sáb","Dom")),ordered = TRUE)netflix_episodios_dia$mesF <- factor(month(netflix_episodios_dia$Date),levels = as.character(1:12), labels = c("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"),ordered = TRUE)netflix_episodios_dia$añomes <- factor(as.yearmon(netflix_episodios_dia$Date)) netflix_episodios_dia$semana <- as.numeric(format(netflix_episodios_dia$Date,"%W"))netflix_episodios_dia$semanames <- ceiling(day(netflix_episodios_dia$Date) / 7)netflix_episodios_dia_calendario <- ggplot(netflix_episodios_dia, aes(semanames, diasemanaF, fill = netflix_episodios_dia$n)) +   geom_tile(colour = "white") +   facet_grid(year(netflix_episodios_dia$Date) ~ mesF) +   scale_fill_gradient(low = "#FFD000", high = "#FF1919") +   ggtitle("Episodios vistos por día en mi Netflix", "Heatmap por día de la semana, mes y año") +  labs(x = "Número de semana", y = "Día de la semana") +  labs(fill = "No.Episodios")netflix_episodios_dia_calendario
What happened in June and July 2017, that a loss of interest on the platform is visible on the plot? Is it related to the trial month that Amazon Prime Video gave me to convince me? Maybe.
We can also see in a very visual and detailed way which are the days of the week that there is more or less activity watching episodes of TV series.
# FREQUENCY OF ACTIVITY IN MY NETFLIX ACCOUNT PER DAYvista_dia <- netflix_episodios_dia %>%  count(diasemanaF)vista_diavista_dia_plot <- vista_dia %>%   ggplot(aes(diasemanaF, n)) +  geom_col(fill = "#5b59d6") +  coord_polar()  +  theme_minimal() +  theme(axis.title.x = element_blank(),        axis.title.y = element_blank(),        axis.text.y = element_blank(),        axis.text.x = element_text(face = "bold"),        plot.title = element_text(size = 16, face = "bold")) +  ggtitle("Frecuencia de episodios vistos", "Actividad por día de la semana en mi Netflix")vista_dia_plot
There is a clear trend, as the plot shows, that Mondays are busy days to spend time on Netflix. Will it be your case too?
Like the previous example, we can repeat the operation but now based on the months, which is the favorite month that we choose to give our valuable time to Netflix.
# FREQUENCY OF ACTIVITY IN MY NETFLIX ACCOUNT PER MONTHvista_mes <- netflix_episodios_dia %>%  count(mesF)vista_mesvista_mes_plot <- vista_mes %>%   ggplot(aes(mesF, n)) +  geom_col(fill = "#808000") +  coord_polar()  +  theme_minimal() +  theme(axis.title.x = element_blank(),        axis.title.y = element_blank(),        axis.text.y = element_blank(),        axis.text.x = element_text(face = "bold"),        plot.title = element_text(size = 18, face = "bold")) +  ggtitle("Frecuencia de episodios vistos", "Actividad por mes en mi Netflix") vista_mes_plot
You will get a plot like the following. The months of June, July, and September have not been the best in my relationship with Netflix.
Finally, let’s review once again in the same format as the two previous examples, your activity but now segmented by the month of each year.
# FREQUENCY OF ACTIVITY IN MY NETFLIX ACCOUNT PER YEARvista_años <- netflix_episodios_dia %>%  count(añomes)vista_añosvista_años_plot <- vista_años %>%   ggplot(aes(añomes, n)) +  geom_col(fill = "#1a954d") +  coord_polar()  +  theme_minimal() +  theme(axis.title.x = element_blank(),        axis.title.y = element_blank(),        axis.text.y = element_blank(),        axis.text.x = element_text(face = "bold"),        plot.title = element_text(size = 18, face = "bold")) +  ggtitle("Frecuencia de episodios vistos", "Actividad por mes del año en mi Netflix")vista_años_plot
You can see in the plot obtained, that in my case the months of October and November 2019 were great for Netflix collecting data about my preferences and habits.
I have to say that I share my account with my girlfriend. Sometimes the recommendations that the platform makes, are not exactly the most appropriate for me or her, lol.
Thanks for your kind reading. You can see the plots generated for this article, a little more fancy with plotly, in the flexdashboard that I put together: https://rpubs.com/cosmoduende/netflix-data-analysis-r
I also share the complete code, in case you are curious to analyze your viewing activity on Netflix: https://github.com/cosmoduende/r-netflix-data-analysis
Have a happy analysis, that you can practice it and even improve it, play, and surprise yourself with interesting results.
This article was translated into English, from the article I previously published in Spanish, at the request of people who do not speak Spanish and who asked me in groups and forums if I would publish it in English.
Thank you and see you soon.
Some rights reserved