R语言每日数据autoplot轴标签与ggseasonplot报错问题求助
Hey there! Let's tackle your two R time series issues one by one.
1. Fixing X-axis to show dates instead of row numbers in autoplot
The root problem here is that converting your xts object to a ts object with as.ts() strips away the actual date information—replacing it with a sequential row index (start=1, end=742). The autoplot function for ts objects uses this index by default for the X-axis.
Here are two simple fixes:
Option 1: Use your xts object directly
The xts package plays nicely with ggfortify's autoplot, so you can skip the ts conversion entirely:
library(ggfortify) autoplot(cw_x[,2:5], facets = TRUE) + ylab("parameters") + xlab("Date")
This will automatically pull the date index from your xts object for the X-axis.
Option 2: Plot directly from the original data frame with ggplot2
If you prefer working with the raw data frame, reshape it to long format and use ggplot2 for full control over axes:
library(ggplot2) library(tidyr) # Reshape data to long format for faceted plotting cw_long <- cw %>% select(date, amp, brg_de_tmp, brg_nde_tmp, kg) %>% pivot_longer(cols = -date, names_to = "Parameter", values_to = "Value") ggplot(cw_long, aes(x = date, y = Value)) + geom_line() + facet_wrap(~Parameter, scales = "free_y") + ylab("parameters") + xlab("Date")
2. Fixing "Data are not seasonal" error for ggseasonplot
The ggseasonplot function (from the forecast package) needs a time series with a defined seasonal frequency to detect patterns. Your current cw_ts has Frequency = 1, which tells R it's annual data with no seasonality—hence the error.
For daily data, we need to set the correct frequency or use a more date-friendly time series structure. Here are two approaches:
Option 1: Create a proper ts object with daily frequency
First, define the start date as a year-month-day tuple, then set frequency = 365 (for non-leap years):
library(forecast) # Get start date components start_date <- as.Date("2016-04-01") start <- c(year(start_date), month(start_date), day(start_date)) # Create ts object with daily frequency cw_daily_ts <- ts(cw[,2:13], start = start, frequency = 365) # Plot seasonplot for a single parameter (e.g., amp) ggseasonplot(cw_daily_ts[,1], year.labels=TRUE, year.labels.left=TRUE) + ylab("amp") + ggtitle("Seasonal plot: Pump Amplitude") # Polar seasonal plot ggseasonplot(cw_daily_ts[,1], polar=TRUE) + ylab("amp") + ggtitle("Polar seasonal plot: Pump Amplitude")
Note: ggseasonplot works best with single time series. To plot multiple parameters, you'll need to loop through them or reshape your data.
Option 2: Use tsibble for intuitive daily time series handling
The tsibble package is built for tidy time series, and pairs with feasts (which has a flexible gg_season function):
library(tsibble) library(feasts) # Convert data frame to tsibble (date as the index) cw_tsibble <- as_tsibble(cw, index = date) # Seasonal plot for a single parameter cw_tsibble %>% gg_season(amp, year.labels = TRUE) + ylab("amp") + ggtitle("Seasonal plot: Pump Amplitude") # Polar seasonal plot cw_tsibble %>% gg_season(amp, polar = TRUE) + ylab("amp") + ggtitle("Polar seasonal plot: Pump Amplitude") # Plot multiple parameters with facets cw_tsibble_long <- cw_tsibble %>% select(date, amp, brg_de_tmp, brg_nde_tmp, kg) %>% pivot_longer(cols = -date, names_to = "Parameter", values_to = "Value") cw_tsibble_long %>% gg_season(Value, year.labels = TRUE) + facet_wrap(~Parameter, scales = "free_y") + ylab("parameters") + ggtitle("Seasonal plots: Pump Parameters")
This approach avoids frequency-setting headaches and is more flexible for daily data.
内容的提问来源于stack exchange,提问作者Avijit Mallick

