geom_smooth 没有出现在 ggplot 上

geom_smooth not appearing on ggplot

我遇到了 geom_smooth() 无法处理我的 ggplot 的问题。根据以前的帖子,我发现因为我的日期变量是字符向量,所以 geom_smooth() 不起作用。我正在尝试将我的日期从 class 字符转换为 class 日期,但使用 as.Date 会导致我的日期变量为 class "unknown"。

这是我尝试修复 class 类型的代码:

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"))

这是我尝试使用 geom_smooth() 绘制的代码,以防有人能帮我找到这里的错误。

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)

最后,这是我的日期列数据示例:

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")

这里是整个数据的一小部分样本:

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"))

试试 lubridate 包:

install.packages("lubridate")
library(lubridate)

然后使用 ymd 函数(查看 dym/ymd order by ?dmy 的文档):

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 = ymd(theatrical_release_release_date))

如果这不起作用,请使用 dput 提供您的数据样本,我会相应地编辑我的答案:)。