【问题标题】:Error in seq.int(r1$year, to$year, by) : 'from' must be a finite numberseq.int(r1$year, to$year, by) 中的错误:'from' 必须是有限数
【发布时间】:2019-04-18 09:39:28
【问题描述】:

这是我的代码 -

uc_ts_plot <- ggplot(monthly_sales, aes(DATE,DAUTONSA)) + geom_line(na.rm=TRUE) + 
+     xlab("Month") + ylab("Auto Sales in Thousands") + 
+     scale_x_date(labels = date_format(format= "%b-%Y"),breaks = date_breaks("1 year")) + 
+     stat_smooth(colour = "green")

uc_ts_plot

错误 -

geom_smooth() using method = 'loess' and formula 'y ~ x' Error in seq.int(r1$year, to$year, by) : 'from' must be a finite number In addition: Warning message: Removed 627 rows containing non-finite values (stat_smooth).

有什么帮助吗?我指的是这个链接 - https://rstudio-pubs-static.s3.amazonaws.com/343096_90b218e393454f79a5012e7ad0913e76.html 但这里的代码不起作用。

【问题讨论】:

标签: r


【解决方案1】:

问题在于将 DATE 列转换为日期格式。

所提供链接上的示例表明:

monthly_sales$DATE <- as.Date(monthly_sales$DATE, "%m/%d/%Y")

而单元格的格式是“1967-01-01”。

因此,所有日期都转换为 NA。

正确的处理方式是:

monthly_sales <- read.csv("DAUTONSA.csv",header = TRUE, stringsAsFactors = FALSE)
monthly_sales$DATE <- as.Date(monthly_sales$DATE, "%Y-%m-%d")

uc_ts_plot <- ggplot(monthly_sales, aes(DATE,DAUTONSA)) + geom_line(na.rm=TRUE) + 
  xlab("Month") + ylab("Auto Sales in Thousands") + 
  scale_x_date(labels = date_format(format= "%b-%Y"),breaks = date_breaks("1 year")) + 
  stat_smooth(colour = "green")

注意变化

monthly_sales$DATE <- as.Date(monthly_sales$DATE, "%Y-%m-%d")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    相关资源
    最近更新 更多