Show me an example of using the autplot function in R
Assistant
Certainly! The autoplot function in R is commonly used with the ggplot2 package and other packages that provide methods for it, such as forecast, , or extensions. It automatically creates a ggplot2 plot for various types of objects (like time series, PCA results, clustering, etc.).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here’s a simple example using the forecast package to plot a time series forecast with autoplot:
Example: Using autoplot with a time series forecast
r
# Install packages if you haven't already
install.packages("forecast")
install.packages("ggplot2")
# Load libraries
library(forecast)
library(ggplot2)
# Create a time series object
ts_data <- ts(AirPassengers, start = c(1949, 1), frequency = 12)
# Fit an ARIMA model
fit <- auto.arima(ts_data)
# Forecast the next 12 months
fc <- forecast(fit, h = 12)
# Plot the forecast using autoplot
autoplot(fc) +
ggtitle("AirPassengers Forecast") +
xlab("Year") +
ylab("Number of Passengers")
What this does:
auto.arima() fits an ARIMA model to the AirPassengers dataset.
forecast() generates a forecast for the next 12 months.
autoplot() automatically creates a ggplot2 plot of the forecast object, including the original data, forecast, and confidence intervals.
If you want an example with ggfortify for a PCA object:
This will create a nice PCA biplot colored by species.
If you want me to show an example for a specific type of object, just let me know!
User
I get this error: Error in autoplot():
! Objects of class are not supported by autoplot.
ℹ Have you loaded the required package?
Run rlang::last_trace() to see where the error occurred.
Assistant
The error message:
code
Error in `autoplot()`:
! Objects of class <ts> are not supported by autoplot.
ℹ Have you loaded the required package?
usually means that the ggfortify package (or another package that provides autoplot methods for time series objects) is not loaded, or you are calling autoplot from ggplot2 directly, which does not support ts objects by default.
How to fix this:
Make sure you have ggfortify installed and loaded, because it extends autoplot to support ts objects:
r
install.packages("ggfortify") # if not installed
library(ggfortify)
Then try your code again:
r
library(forecast)
library(ggfortify)
ts_data <- ts(AirPassengers, start = c(1949, 1), frequency = 12)
fit <- auto.arima(ts_data)
fc <- forecast(fit, h = 12)
autoplot(fc) +
ggtitle("AirPassengers Forecast") +
xlab("Year") +
ylab("Number of Passengers")
Why this happens:
The autoplot function is a generic function with methods for different object classes.
The base ggplot2::autoplot() only supports a few classes.
The forecast package itself does not export an autoplot method for forecast objects.
The ggfortify package provides autoplot methods for many classes, including ts and forecast objects.
Summary:
Install and load ggfortify.
Then autoplot() will work with ts and forecast objects.
If you want, here is a minimal reproducible example that should work:
r
install.packages(c("forecast", "ggfortify"))
library(forecast)
library(ggfortify)
ts_data <- ts(AirPassengers, start = c(1949, 1), frequency = 12)
fit <- auto.arima(ts_data)
fc <- forecast(fit, h = 12)
autoplot(fc)
Let me know if you want me to help with any other specific use case!
User
I want to have it work with tsibble
Assistant
Great! If you want to use autoplot() with tsibble objects (from the tsibble package, which is part of the tidyverts ecosystem for tidy time series analysis), the approach is a bit different because:
autoplot() from ggfortify or forecast does not support tsibble objects directly.
Instead, you typically use fable and feasts packages for modeling and plotting with tsibble data.
For plotting, you can use autoplot() from feasts or just use ggplot2 directly on tsibble data.
Example: Using autoplot() with tsibble and fable
r
# Install required packages if you haven't already
install.packages(c("tsibble", "fable", "feasts", "ggplot2"))
# Load libraries
library(tsibble)
library(fable)
library(feasts)
library(ggplot2)
# Use the built-in tsibble dataset: aus_production
data("aus_production", package = "fpp3")
# Filter for a single series (e.g., Beer production)
beer_tsibble <- aus_production %>%
select(Quarter, Beer) %>%
filter(!is.na(Beer))
# Fit an ETS model (Exponential smoothing)
fit <- beer_tsibble %>%
model(ETS(Beer))
# Forecast next 8 quarters
fc <- fit %>% forecast(h = "2 years")
# Plot the forecast using autoplot from feasts/fable
autoplot(fc) +
autolayer(beer_tsibble, Beer) +
ggtitle("Beer Production Forecast") +
xlab("Year") +
ylab("Beer Production")
Explanation:
aus_production is a tsibble dataset included in fpp3 (which loads tsibble, fable, feasts, etc.).
We fit an ETS model to the Beer production time series.
We forecast 2 years ahead.
autoplot() from fable/ feasts works with forecast objects created by forecast() from fable.
autolayer() adds the original data to the plot.
Notes:
If you don’t have fpp3 installed, you can install it with:
r
install.packages("fpp3")
fpp3 is a meta-package that installs and loads tsibble, fable, feasts, and other tidyverts packages.
If you want me to help with a specific tsibble dataset or model, just ask!