I am having a problem where geom_smooth() is not working on my ggplot. Based on previous posts, I have found that because my date variables are character vectors, the geom_smooth() is not working. I am trying to convert my dates from class character to class date but using as.Date results in a class "unknown" for my date variables.
Here is my code to attempt to fix my class type:
allmovies <- allmovies %>%
clean_names() %>%
select(movie, total_box_office, theatrical_release_release_date,
running_time, mpaa, metacritic, sentiment) %>%
mutate(theatrical_release_release_date =
as.character(theatrical_release_release_date)) %>%
mutate(theatrical_release_release_date = as.Date(theatrical_release_release_date, format = "%Y-%m-%d"))
And here is my code for attempting to plot with geom_smooth(), in case anyone can help me find the error here.
ggplotly(tooltip = c("text"),
ggplot(data = allmovies, aes(x = theatrical_release_release_date,
y = total_box_office, color = mpaa, text = movie)) +
geom_point() +
geom_smooth(method=lm) +
scale_y_continuous(labels = comma) +
labs(color = "MPAA Rating") +
ylab("Total Box Office Revenue") +
xlab("Theatrical Release Date") +
ggtitle("Total Box Office Revenue Over Time",
subtitle = "While revenue generally improved over time, a further analysis shows PG rated movies generated much more revenue over time while PG-13 and R-rated revenue correlations do not appear to be significant.")) %>%
layout(title = "Total Box Office Revenue Over Time",
font = font)
Finally, here is a sample of my data of the date column:
dput(head(allmovies$theatrical_release_release_date))
c("2013-08-23", "2013-03-22", "2012-09-14", "2012-03-16", "2012-02-17",
"2011-10-14")
and here is a small sample of the whole data:
structure(list(movie = c("The Frozen Ground", "The Croods", "Stolen", "Seeking Justice", "Ghost Rider: Spirit of Vengeance", "Trespass" ), total_box_office = c(5617460, 573068425, 17967746, 411746, 149217355, 786532), theatrical_release_release_date = structure(c(15940, 15786, 15597, 15415, 15387, 15261), class = "Date"), running_time = c(105, 98, 96, 104, 95, 90), mpaa = c("R", "PG", "R", "R", "PG-13", "R"), metacritic = c(37, 55, 43, 38, 34, 37), sentiment = c(NA, 0.1363636, NA, NA, NA, NA)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))