Warning: package 'tidyverse' was built under R version 4.6.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dslabs)
Warning: package 'dslabs' was built under R version 4.6.2
library(dplyr)#list all the datasets under dslabs#data(package = "dslabs")
Picking a dataset and viewing it
After viewing the datasets available in the dslabs package, I would like to create a graph using the movielens dataset, which is data based on ratings for movies. I would like to find out which genre has the highest ratings over time, therefore I plan to create a scatterplot.
#load the datasetdata("movielens")#look at the datastr(movielens)
movieId title year
1 31 Dangerous Minds 1995
2 1029 Dumbo 1941
3 1061 Sleepers 1996
4 1129 Escape from New York 1981
5 1172 Cinema Paradiso (Nuovo cinema Paradiso) 1989
6 1263 Deer Hunter, The 1978
genres userId rating timestamp
1 Drama 1 2.5 1260759144
2 Animation|Children|Drama|Musical 1 3.0 1260759179
3 Thriller 1 3.0 1260759182
4 Action|Adventure|Sci-Fi|Thriller 1 2.0 1260759185
5 Drama 1 4.0 1260759205
6 Drama|War 1 2.0 1260759151
#view(movielens)
Filtering the data
After looking at the dataset, the genres column is really complicated because it has a lot of subsets, so I will filter the data so that it only has the top three genres.
#find the top three most popular genresgenre_count <- movielens |>group_by(genres) |>summarise(count =n()) |>filter(count >3500) # I played around with the count until it only showed three genresgenre_count
# A tibble: 3 × 2
genres count
<fct> <int>
1 Comedy 6748
2 Comedy|Romance 3973
3 Drama 7757
Plot the graph
For the scatter-plot, the x-axis will be years, the y-axis will be ratings, and the points on the graph will be ratings from each movie under the top three genres: Comedy, Comedy|Romance, and Drama.
#filter movielens to only have the top three genrestop_three <- movielens |>filter(genres %in%c("Comedy","Comedy|Romance","Drama"))top_three |>group_by(genres) |>summarise(avg_rating =mean(rating))
# A tibble: 3 × 2
genres avg_rating
<fct> <dbl>
1 Comedy 3.27
2 Comedy|Romance 3.37
3 Drama 3.71
#plot the graphggplot(top_three, aes(x=year, y=rating, color=genres))+geom_point()
Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
#add labels, change color scheme and themeggplot(top_three, aes(x=year, y=rating, color=genres))+geom_point()+labs(x ="Year", y ="Rating",title ="Top Three Movie Genres by Year",caption ="Source: DS Labs") +scale_color_brewer(name ="Genre", palette ="Set1") +theme_minimal(base_family ="mono")
Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
Through the movielens dataset, I used three variables to plot my graph: genres, year, and rating. I built the graph by filtering the genres column to only have the top three genres according to the number of reviews it got. After this I created a scatter-plot to have the year variable on the x-axis, rating on the y-axis, and then color coded the points on the graph to be by genre, as seen on the legend.
Visually, I can see a large concentration of drama movies (green dots) were released between 1950 and 1980. After 1980, comedy movies (red dots) dominated with some rom-coms (blue dots) pushing through. But, after the 2000s, it’s mostly drama movies that tend to have high ratings, as seen in the large concentration of green in the top right.
Bonus - most reviewed movies + their average ratings
# A tibble: 7 × 2
title count
<chr> <int>
1 Forrest Gump 341
2 Jurassic Park 274
3 Matrix, The 259
4 Pulp Fiction 324
5 Shawshank Redemption, The 311
6 Silence of the Lambs, The 304
7 Star Wars: Episode IV - A New Hope 291
most_reviewed <- movielens |>filter(title %in%c("Forrest Gump","Jurassic Park","Matrix, The", "Pulp Fiction","Shawshank Redeption, The", "Silence of the Lambs, The", "Star Wars: Episode IV - A New Hope"))most_reviewed |>group_by(title) |>summarise(avg_rating =mean(rating))
# A tibble: 6 × 2
title avg_rating
<chr> <dbl>
1 Forrest Gump 4.05
2 Jurassic Park 3.71
3 Matrix, The 4.18
4 Pulp Fiction 4.26
5 Silence of the Lambs, The 4.14
6 Star Wars: Episode IV - A New Hope 4.22
#plot the graphggplot(most_reviewed, aes(x=title))+geom_bar()+labs(x ="Movie Title", y ="Number of Reviews",title ="Most Reviewed Movies",caption ="Source: DS Labs")+theme(axis.text.x =element_text(angle=45, hjust=1))