prophet_plot_components() behaves differently from ggplot2 object

Created on 11 Dec 2017  路  3Comments  路  Source: facebook/prophet

prophet_plot_components() behaves differently from the object of ggplot2.
Especially, it is clear to use prophet_plot_components() in *.Rmd file.
Actually, It is not a serious problem as long as you use prophet only in *.R file.

For example, only prophet_plot_components() is plotted in the the following simple R Markdown code using prophet:

---
output: html_document
---

```{r}
library(ggplot2)
library(dplyr)
library(prophet)
df <- read.csv("https://raw.githubusercontent.com/facebook/prophet/master/examples/example_wp_R.csv") %>%
  dplyr::mutate(y = log(y))
m <- prophet(df)
forecast <- predict(m, make_future_dataframe(m, periods = 365))
for(i in 1:2){
  plot(m, forecast)
  prophet_plot_components(m, forecast)
}

</pre>

But in this case, `plot(m, forecast)` is not plotted like usual `ggplot2 object`.
It is OK for tidyverse guys because it is a specification of ggplot2 package.

If we modify the plotted part of the above code like this:
```r
for(i in 1:2){
  print(plot(m, forecast))
  prophet_plot_components(m, forecast)
}

It works well as we usually hope. But, if you write like this

for(i in 1:2){
  print(plot(m, forecast))
  print(prophet_plot_components(m, forecast))
}

Redundant plots will be shown by plot invisible panels object.
This behavior is different from ggplot2 object, and it will cause a little confusion.

Does that make sense?
If it is for you, I would like to make a PR to fix this problem.
My idea is that prophet_plot_components() returns ggplot2 compatible object.

All 3 comments

This is correct, prophet_plot_components does not return a ggplot2 object. Rather it returns an invisible list of ggplot2 plots. The "grid" package is used to put these all together into a single plot. This is because ggplot2 as far as I am aware does not allow putting multiple subplots with different axes into a single plot.

If you figured out a way to get these into a ggplot2 object that would be amazing. (But I'm not sure it's possible).

I got an advice from my friend who is a great R programmer.
And he said that we might use gtable for returning the object as ggplot2 object.

I will try this and make a PR after implementation :)

I'm going to close this in the meantime since there isn't anything to do about this immediately.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xiaoyaoyang picture xiaoyaoyang  路  3Comments

datafool picture datafool  路  3Comments

annabednarska picture annabednarska  路  3Comments

robertdknight picture robertdknight  路  3Comments

germayneng picture germayneng  路  3Comments