Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autoplot does not accept ts object

Tags:

r

ggplot2

I am creating a ts object and then I am trying to run it through autoplot. Execution gives me an error:

autoplot(pts, facets = TRUE) Error: Objects of type mts/ts/matrix not supported by autoplot.

I have already checked the type of the object , and it is ts and autoplot is supposed to make a plot out of the ts object. I also tried to run other built in ts object (USAccDeaths) , but it gives me same error

library(ggplot2)

pts <- ts(data = Popcopys[,-1], start = c(2006,1),frequency = 1 )

autoplot(pts) autoplot(USAccDeaths)

A plot of TS is expected , but what I get is this error:

autoplot(pts) Error: Objects of type mts/ts/matrix not supported by autoplot. autoplot(USAccDeaths) Error: Objects of type ts not supported by autoplot.

like image 370
Fahadakbar Avatar asked Apr 11 '19 14:04

Fahadakbar


People also ask

What package is autoplot in R?

The single function in the ggfortify package, which is of interest to us is the autoplot() function, which is helpful to plot time series objects (i.e a ts object) the ggplot way.

How do I add a title to autoplot?

To add an arbitrary title and/or subtitle to a plot, use either function ggtitle() or function labs() from package 'ggplot2'. For example "title:objt" , the default for "title" , adds a title with the name of the object being plotted.


1 Answers

This works:

library(ggplot2)
library(ggfortify)
autoplot(USAccDeaths)

Following https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_ts.html :
"{ggfortify} lets {ggplot2} know how to interpret ts objects"

like image 178
Robert Avatar answered Nov 11 '22 06:11

Robert