# Calculating the average rating and number of ratings for each moviemovies_summary <- movielens %>%group_by(movieId, title, year, genres) %>%summarise(avg_rating =mean(rating, na.rm =TRUE),rating_count =n(),.groups ="drop" )# Previewing the summarized movie datahead(movies_summary)
# A tibble: 6 × 6
movieId title year genres avg_rating rating_count
<int> <chr> <int> <fct> <dbl> <int>
1 1 Toy Story 1995 Adventure|A… 3.87 247
2 2 Jumanji 1995 Adventure|C… 3.40 107
3 3 Grumpier Old Men 1995 Comedy|Roma… 3.16 59
4 4 Waiting to Exhale 1995 Comedy|Dram… 2.38 13
5 5 Father of the Bride Part II 1995 Comedy 3.27 56
6 6 Heat 1995 Action|Crim… 3.88 104
Filtering Movies into Five Genre Categories
# Separating movies with multiple genres into individual categoriesmovies_filtered <- movies_summary %>%mutate(genre =as.character(genres)) %>%separate_rows(genre, sep ="\\|") %>%filter(genre %in%c("Action", "Comedy", "Drama", "Horror", "Romance")) %>%filter(!is.na(year) &!is.na(avg_rating) &!is.na(rating_count))# Checking the genre categories and number of moviestable(movies_filtered$genre)
Action Comedy Drama Horror Romance
1543 3305 4327 872 1541
nrow(movies_filtered)
[1] 11588
Visualizing the Movie Ratings Across the Release Years and Genres
# Creating a scatterplot that compares movie release years and average ratingsggplot(movies_filtered, aes(x = year, y = avg_rating,color = genre, size = rating_count)) +geom_point(alpha =0.5) +scale_color_brewer(palette ="Set1") +scale_size(range =c(1, 7)) +theme_minimal() +labs(title ="Movie Ratings Across Release Years and Genres",subtitle ="Comparing average ratings, genres, and popularity",x ="Movie Release Year",y ="Average Movie Rating (1–5)",color ="Movie Genre",size ="Number of Ratings",caption ="Source: DS Labs MovieLens Dataset" )
Analysis and Interpretation
I decided to use the MovieLens dataset from the DS Labs package for this assignment. This dataset contained information about movie ratings, release years, and genres. I decided to first group the ratings by movie so that I could calculate each movie’s average rating and the number of ratings it had received.
Some of the movies belonged to multiple genres, and so I separated those categories and focused on action, comedy, drama, horror, and romance. I was also able to remove observations with missing values before I created my visualization.
My scatter plot compares the movie release years and average ratings, and it uses different colors to represent genres. Larger points represent movies that have more ratings. Something that I found interesting was that there are considerably more movie observations from recent years compared to older years. Something else that I also noticed is that average ratings vary quite a bit within each genre, which suggests to me that genre alone does not determine how highly a movie is rated.
Overall, though, the graph shows how multiple variables can be combined to better understand patterns in movie ratings.