You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

autoplot报错:numeric类型不支持,如何绘制经济舱乘客随时间变化图?

Fixing the autoplot Error for Numeric Data

Ah, I see the issue—you’re getting that error because autoplot() loves working with structured time series objects (like ts, zoo, or xts), but your Economy.Class column is just a plain old numeric vector. No worries, we can fix this in two easy ways:

Solution 1: Turn Your Numeric Column into a Time Series

Wrap your Economy.Class data in the ts() function to convert it into a proper time series object. I’ll assume each row in your dataset represents a consecutive year starting from year 1—if you know the actual starting year (like 1985), just swap out the start value:

# Load the libraries you need
library(ggplot2)
library(forecast) # autoplot comes from this package

# Convert the numeric column to a time series
economy_time_series <- ts(melsyd$Economy.Class, start = 1)

# Now autoplot will work perfectly!
autoplot(economy_time_series) +
  ggtitle("Economy class passengers: Melbourne-Sydney") +
  xlab("Year") +
  ylab("Thousands")

Solution 2: Use ggplot2 Directly (No Time Series Conversion Required)

If you’d rather skip the time series conversion, you can build the plot straight with ggplot2 by creating a year variable from your dataset’s row numbers:

library(ggplot2)

# Add a Year column to your dataset (each row = one year)
melsyd$Year <- seq(nrow(melsyd))

# Plot the trend with geom_line
ggplot(melsyd, aes(x = Year, y = Economy.Class)) +
  geom_line(color = "steelblue", linewidth = 1) # Optional: make the line look nicer
  ggtitle("Economy class passengers: Melbourne-Sydney") +
  xlab("Year") +
  ylab("Thousands")

Both methods will give you the time series plot you’re after. The first sticks with your original autoplot approach, while the second gives you more control over how the plot looks using ggplot2’s standard tools.

内容的提问来源于stack exchange,提问作者noob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 10:08:16