Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Autoplot function and decomposition

I am using the forecast package and I tried to make a decomposition plot. Below you can see the code.

library(forecast)
librarary(ggplot2)
stl(AirPassengers,s.window="periodic",robust=TRUE)%>%autoplot()

These lines of code produce a plot with four charts below you can see the plot

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now I want to have this plot but without the first chart (data). So can anybody help me how to solve this problem and make a plot only with three charts (trend, seasonal, and remainder )?

>Solution :

One option would be to manually manipulate the ggplot2 object returned by autoplot, e.g. to get rid of the data panel you could remove the data values from the plot data and the layer data.

As the categories for the facets are stored in a column called parts we have to filter the data to get rid of the parts category "data":

library(forecast)
library(ggplot2)

p <- stl(AirPassengers,s.window="periodic",robust=TRUE) |> autoplot()

# Remove "data" from layer data
p$layers <- lapply(p$layers, function(x) { x$data <- subset(x$data, parts != "data"); x })
# Remove "data" from plot data
p$data <- subset(p$data, parts != "data")

p

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading