geom_smooth没有出现在ggplot上

时间:2018-11-27 18:45:36

标签: r ggplot2 dplyr tidyverse as.date

我在我的ggplot上无法使用geom_smooth()时遇到问题。根据以前的帖子,我发现由于我的日期变量是字符向量,geom_smooth()无法正常工作。我正在尝试将日期从班级字符转换为班级日期,但是使用as.Date导致日期变量的类“未知”。

这是我的代码,试图修复我的班级类型:

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

1 个答案:

答案 0 :(得分:-1)

尝试lubridate软件包:

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

然后使用ymd函数(请按?dmy查阅dym / ymd顺序的文档):

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来提供您的数据样本不起作用,我将相应地编辑答案:)。