首页 > 解决方案 > 无法使用自动绘图可视化“ts” - 错误:提供连续比例的离散值

问题描述

我是新来的。我将列转换为ts,但无法使用autoplot

library(tidyverse)
library(ggfortify)
library(forecast)

test <- ts(raw_mongolia_monthly$Time_Period, start = 2014, frequency = 12)

         Jan     Feb     Mar     Apr     May     Jun     Jul     Aug     Sep     
Oct     Nov     Dec
2014 2014/01 2014/02 2014/03 2014/04 2014/05 2014/06 2014/07 2014/08 2014/09 
2014/10 2014/11 2014/12
2015 2015/01 2015/02 2015/03 2015/04 2015/05 2015/06 2015/07 2015/08 2015/09 
2015/10 2015/11 2015/12
2016 2016/01 2016/02 2016/03 2016/04 2016/05 2016/06 2016/07 2016/08 2016/09 
2016/10 2016/11 2016/12
2017 2017/01 2017/02 2017/03 2017/04 2017/05 2017/06   

class(test)
[1] "ts"

 autoplot(test) 

**Error: Discrete value supplied to continuous scale**

我的数据

> dput(test)
structure(c("2014/01", "2014/02", "2014/03", "2014/04", "2014/05", 
"2014/06", "2014/07", "2014/08", "2014/09", "2014/10", "2014/11", 
"2014/12", "2015/01", "2015/02", "2015/03", "2015/04", "2015/05", 
"2015/06", "2015/07", "2015/08", "2015/09", "2015/10", "2015/11", 
"2015/12", "2016/01", "2016/02", "2016/03", "2016/04", "2016/05", 
"2016/06", "2016/07", "2016/08", "2016/09", "2016/10", "2016/11", 
"2016/12", "2017/01", "2017/02", "2017/03", "2017/04", "2017/05", 
"2017/06"), .Tsp = c(2014, 2017.41666666667, 12), class = "ts")

检查了多个链接,但仍然不起作用。看不到适合我的解决方案。

我唯一设法用这些数据做的事情没有转换成ts- 用 ggplot 可视化geom_point,使用

ggplot(raw_mongolia_monthly, aes(Time_Period, Value)) + geom_point()

谢谢!

标签: rtime-series

解决方案


问题中显示的图与显示的测试数据不对应。测试数据是 ts 系列字符数据,而绘图显示绘制的数字数据。

我怀疑这个问题存在错误,但从表面上看,它要求对自己绘制一些年/月,因此将数据转换为一个yearmon对象,然后从中创建一个动物园对象。现在autoplot可以工作了。

library(zoo)

ym <- as.yearmon(coredata(test), "%Y/%m")
z <- zoo(ym, ym)
autoplot(z) + scale_x_yearmon() + scale_y_yearmon()

给予:

截屏


推荐阅读