【问题标题】:How to change type of line in prophet plot?如何更改先知情节中的线条类型?
【发布时间】:2018-12-27 15:52:47
【问题描述】:

Facebook 的 Prophet R(还有 Python 版本)用于生成时间序列预测。

模型 m 由以下人员创建:

m <- prophet(df)
future <- make_future_dataframe(m, periods = 365)
forecast <- predict(m, future)
plot(m, forecast)

返回一个格式很好的图表,例如:

我想改变线型,不是点,而是一条通常的细线。

我试过了

lines(m$history$y,lty=1)

但是出错了

 In doTryCatch(return(expr), name, parentenv, handler)

对如何将这些点转换成一条线有什么建议吗?

【问题讨论】:

    标签: r facebook-prophet


    【解决方案1】:

    prophet 对象的plot 方法使用ggplot2,因此像lines() 这样的基本R 图形函数将不起作用。您可以使用ggplot2::geom_line()添加行,但目前我没有看到一种简单的方法来替换行...

    来自?prophet的示例:

    history <- data.frame(ds = seq(as.Date('2015-01-01'), as.Date('2016-01-01'), by = 'd'),
                               y = sin(1:366/200) + rnorm(366)/10)
         m <- prophet(history)
    future <- make_future_dataframe(m, periods = 365)
    forecast <- predict(m, future)
    pp <- plot(m,forecast)
    

    添加行:

    library(ggplot2)
    pp + geom_line()
    

    This question 提供了一个(hacky)前进的方向:

    pp2 <- pp + geom_line()
    qq2 <- ggplot_build(pp2)
    qq2$data[[2]]$colour <- NA
    plot(ggplot_gtable(qq2))
    

    但很明显,这个 hack 出了点问题。更好的选择是查看绘图方法(prophet:::plot.prophet)并修改它以按照您的意愿行事......这是基本版本:

    df <- prophet:::df_for_plotting(m, forecast)
    gg <-ggplot(df, aes(x = ds, y = y)) + labs(x = "ds", y = "y")
    gg <- gg + geom_ribbon(ggplot2::aes(ymin = yhat_lower, 
            ymax = yhat_upper), alpha = 0.2, fill = "#0072B2", 
            na.rm = TRUE)
    ## replace first geom_point() with geom_line() in next line ...
    gg <- gg + geom_line(na.rm = TRUE) + geom_line(aes(y = yhat), 
        color = "#0072B2", na.rm = TRUE) + theme(aspect.ratio = 3/5)
    

    我可能已经删除了您的数据/预测中存在的一些组件...

    【讨论】:

    • 非常感谢,但是否可以使用dyplot.prophet(m, forecast)(情节的html版本)进行此类操作?
    【解决方案2】:

    可以使用dyplot.prophet(m, forecast) (html 版本的情节) 进行这样的操作:) 在此之前,我们应该像这里这样重写函数:

    dyplot.prophet <- function(x, fcst, uncertainty=TRUE, 
                               ...) 
    {
      forecast.label='Predicted'
      actual.label='Actual'
      # create data.frame for plotting
      df <- prophet:::df_for_plotting(x, fcst)
    
      # build variables to include, or not, the uncertainty data
      if(uncertainty && exists("yhat_lower", where = df))
      {
        colsToKeep <- c('y', 'yhat', 'yhat_lower', 'yhat_upper')
        forecastCols <- c('yhat_lower', 'yhat', 'yhat_upper')
      } else
      {
        colsToKeep <- c('y', 'yhat')
        forecastCols <- c('yhat')
      }
      # convert to xts for easier date handling by dygraph
      dfTS <- xts::xts(df %>% dplyr::select_(.dots=colsToKeep), order.by = df$ds)
    
      # base plot
      dyBase <- dygraphs::dygraph(dfTS)
    
      presAnnotation <- function(dygraph, x, text) {
        dygraph %>%
          dygraphs::dyAnnotation(x, text, text, attachAtBottom = TRUE)
      }
    
      dyBase <- dyBase %>%
        # plot actual values
        dygraphs::dySeries(
          'y', label=actual.label, color='black',stepPlot = TRUE, strokeWidth=1
        ) %>%
        # plot forecast and ribbon
        dygraphs::dySeries(forecastCols, label=forecast.label, color='blue') %>%
        # allow zooming
        dygraphs::dyRangeSelector() %>% 
        # make unzoom button
        dygraphs::dyUnzoom()
      if (!is.null(x$holidays)) {
        for (i in 1:nrow(x$holidays)) {
          # make a gray line
          dyBase <- dyBase %>% dygraphs::dyEvent(
            x$holidays$ds[i],color = "rgb(200,200,200)", strokePattern = "solid")
          dyBase <- dyBase %>% dygraphs::dyAnnotation(
            x$holidays$ds[i], x$holidays$holiday[i], x$holidays$holiday[i],
            attachAtBottom = TRUE)
        }
      }
      return(dyBase)
    }
    

    strokeWidth=0 是以前的,我们已将其更改为 strokeWidth=1 并添加了 stepPlot = TRUE

    整个基础代码都在这里:https://rdrr.io/cran/prophet/src/R/plot.R

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      相关资源
      最近更新 更多