This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
movie_data <- data.frame(
person = c(
"Alice","Alice","Alice","Alice","Alice",
"Brian","Brian","Brian","Brian","Brian",
"Carla","Carla","Carla","Carla","Carla",
"David","David","David","David","David",
"Elena","Elena","Elena","Elena","Elena"
),
movie = c(
"Spider-Man: Brand New Day","The Odyssey","Toy Story 5",
"The Super Mario Galaxy Movie","Project Hail Mary",
"Spider-Man: Brand New Day","The Odyssey","Toy Story 5",
"The Super Mario Galaxy Movie","Project Hail Mary",
"Spider-Man: Brand New Day","The Odyssey","Toy Story 5",
"The Super Mario Galaxy Movie","Project Hail Mary",
"Spider-Man: Brand New Day","The Odyssey","Toy Story 5",
"The Super Mario Galaxy Movie","Project Hail Mary",
"Spider-Man: Brand New Day","The Odyssey","Toy Story 5",
"The Super Mario Galaxy Movie","Project Hail Mary"
),
rating = c(
5,4,4,5,4,
4,5,3,4,5,
5,4,5,4,4,
3,5,4,5,5,
4,4,5,4,3
)
)
View(movie_data)
library(ggplot2)
library(dplyr)
##
## 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
# Calculate average rating for each movie
movie_summary <- movie_data %>%
group_by(movie) %>%
summarise(
average_rating = mean(rating)
)
# View summary
movie_summary
## # A tibble: 5 × 2
## movie average_rating
## <chr> <dbl>
## 1 Project Hail Mary 4.2
## 2 Spider-Man: Brand New Day 4.2
## 3 The Odyssey 4.4
## 4 The Super Mario Galaxy Movie 4.4
## 5 Toy Story 5 4.2
# Create bar graph
ggplot(movie_summary,
aes(x = reorder(movie, average_rating),
y = average_rating)) +
geom_col() +
coord_flip() +
labs(
title = "Average Ratings for Five Popular Movies",
x = "Movie",
y = "Average Rating"
) +
ylim(0, 5)
ggplot(movie_summary,
aes(x = movie, y = average_rating)) +
geom_col() +
labs(
title = "Average Movie Ratings",
x = "Movie",
y = "Average Rating"
) +
ylim(0, 5) +
theme(
axis.text.x = element_text(
angle = 45,
hjust = 1
)
)