I wonder if there is a function to plot ts
object directly with ggplot2. In past, I was using the following strategy but now it is throwing an error.
我想知道是否有一个函数可以直接用ggplot2绘制ts对象。在过去,我使用以下策略,但现在它抛出错误。
set.seed(12345)
dat <- ts(data=runif(n=10, min=50, max=100), frequency = 4, start = c(1959, 2))
df <- data.frame(date=as.Date(time(dat)), Y=as.matrix(dat))
library(ggplot2)
ggplot(data=df, mapping=aes(x=date, y=Y))+geom_point()
Error
Error in as.Date.default(time(dat)) :
do not know how to convert 'time(dat)' to class “Date”
I would highly appreciate if someone guide me how to directly plot ts
object with ggplot2
. Thanks in advance for your help.
如果有人指导我如何使用ggplot2直接绘制ts对象,我将非常感激。在此先感谢您的帮助。
3 个解决方案
#1
8
Try this:
library(zoo)
library(ggplot2)
library(scales)
autoplot(as.zoo(dat), geom = "point")
or maybe:
autoplot(as.zoo(dat), geom = "point") + scale_x_yearqtr()
See ?autoplot.zoo
for more info.
有关详细信息,请参阅?autoplot.zoo。
Note: By the way, the code in the question works if you issue the command library(zoo)
first.
注意:顺便说一句,如果您首先发出命令库(zoo),问题中的代码将起作用。
Updates Added second solution, library(scales)
and switched from yearmon
to yearqtr
.
更新添加了第二个解决方案,库(scale)并从yearmon切换到yearqtr。
#2
5
Don't know why it worked before (since it would not seem to be valid under my understanding of Date functins) but you can fix it with the use of zoo::as.yearqtr
不知道为什么它之前有效(因为根据我对Date functins的理解它似乎没有效果)但是你可以使用zoo :: as.yearqtr修复它
library(zoo)
?as.yearqtr
set.seed(12345)
dat <- ts(data=runif(n=10, min=50, max=100), frequency = 4, start = c(1959, 2))
df <- data.frame(date=as.Date(as.yearqtr(time(dat))), Y=as.matrix(dat))
library(ggplot2)
ggplot(data=df, mapping=aes(x=date, y=Y))+geom_point()
# No errors. The plot has YYYY-MM labeling as expected for a ggplot2-Date axis.
#3
2
This code works for me
这段代码适合我
set.seed(12345)
dat <- ts(data=runif(n=10, min=50, max=100), frequency = 4, start = c(1959, 2))
library(ggfortify)
autoplot(dat, geom = "point", ts.colour = ('dodgerblue3')) #Option 1
library(zoo)
autoplot.zoo(as.zoo(dat), geom = "point") #Option 2
#1
8
Try this:
library(zoo)
library(ggplot2)
library(scales)
autoplot(as.zoo(dat), geom = "point")
or maybe:
autoplot(as.zoo(dat), geom = "point") + scale_x_yearqtr()
See ?autoplot.zoo
for more info.
有关详细信息,请参阅?autoplot.zoo。
Note: By the way, the code in the question works if you issue the command library(zoo)
first.
注意:顺便说一句,如果您首先发出命令库(zoo),问题中的代码将起作用。
Updates Added second solution, library(scales)
and switched from yearmon
to yearqtr
.
更新添加了第二个解决方案,库(scale)并从yearmon切换到yearqtr。
#2
5
Don't know why it worked before (since it would not seem to be valid under my understanding of Date functins) but you can fix it with the use of zoo::as.yearqtr
不知道为什么它之前有效(因为根据我对Date functins的理解它似乎没有效果)但是你可以使用zoo :: as.yearqtr修复它
library(zoo)
?as.yearqtr
set.seed(12345)
dat <- ts(data=runif(n=10, min=50, max=100), frequency = 4, start = c(1959, 2))
df <- data.frame(date=as.Date(as.yearqtr(time(dat))), Y=as.matrix(dat))
library(ggplot2)
ggplot(data=df, mapping=aes(x=date, y=Y))+geom_point()
# No errors. The plot has YYYY-MM labeling as expected for a ggplot2-Date axis.
#3
2
This code works for me
这段代码适合我
set.seed(12345)
dat <- ts(data=runif(n=10, min=50, max=100), frequency = 4, start = c(1959, 2))
library(ggfortify)
autoplot(dat, geom = "point", ts.colour = ('dodgerblue3')) #Option 1
library(zoo)
autoplot.zoo(as.zoo(dat), geom = "point") #Option 2