R:如何绘制“时间序列"?与ggplot2 [英] R: how to plot a "time series" with ggplot2

查看:71
本文介绍了R:如何绘制“时间序列"?与ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中模拟了一些时间序列数据,每8天汇总一次,然后尝试绘制结果.但是,这些图不起作用.我已经在下面发布了我的代码:

I simulated some time series data in R, aggregated by every 8 days, and then tried to plot the results. However, the plots are not working. I have posted my code below:

#load library
library(xts)
library(ggplot2)

set.seed(123)
    
#simulate data
    property_damages_in_dollars <- rnorm(731,100,10)

date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
    
    date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")
    
final_data <- data.frame(date_decision_made, property_damages_in_dollars)

#convert to xts object
dat <- xts(final_data$property_damages_in_dollars, 
           as.Date(final_data$date_decision_made, '%Y/%m/%d'))

#aggregate by 8 day period
ep <- endpoints(dat,'days',k=8)

a = period.apply(x=dat,ep,FUN=sum )

#plot : does not work
plot(a)

#plot using ggplot :does not work

ggplot(a, aes(x = a[,1], y=a$a))+
         geom_line(aes(group=1))+
  scale_x_discrete(guide = guide_axis(n.dodge=2))+
  theme(axis.text.x = element_text(angle = 45))

有人可以告诉我我在做什么错吗?

Could someone please tell me what I am doing wrong?

谢谢

推荐答案

autoplot(a, geom = "point")

自动绘图似乎有效.更多信息

Autoplot seems to work. More info here.

这篇关于R:如何绘制“时间序列"?与ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆