R:将日期从每天转换为每周,然后进行绘制

编程入门 行业动态 更新时间:2024-10-11 17:29:31
本文介绍了R:将日期从每天转换为每周,然后进行绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试学习如何处理时间序列数据.我创建了一些虚假的每日数据,尝试按周汇总,然后将其绘制出来:

I am trying to learn how to deal with time series data. I created some fake daily data, tried to aggregate it by week and then plot it:

set.seed(123) library(xts) library(ggplot2) 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") property_damages_in_dollars <- rnorm(731,100,10) final_data <- data.frame(date_decision_made, property_damages_in_dollars) y.mon<-aggregate(property_damages_in_dollars~format(as.Date(date_decision_made), format="%W"),data=final_data, FUN=sum) y.mon$week = y.mon$`format(as.Date(date_decision_made), format = "%W")` g = ggplot(y.mon, aes(x = week, y=property_damages_in_dollars) + geom_line(aes(group=1))

该图似乎有效,但是只有52个滴答声"轴上的数量应该是该数量的两倍(有2年的数据).我认为将数据从每天转换为每周时会出现问题-有人可以告诉我如何解决此问题吗?

The plot seems to work, but there are only 52 "ticks" on the axis when there should be twice that amount (there are 2 years of data). I think that there is a problem when converting the data from daily to weekly - could someone please show me how to fix this?

在我的实际数据中,我有30年的数据.日期似乎很拥挤.我试图退缩".日期:

In my actual data, I have 30 years of data. The dates appear to be quite crowded. I tried to "uncrowd" the dates:

library(scales) g + scale_x_date(date_breaks = "1 week", expand = c(0,0)) + theme(axis.text.x = element_text(angle=90, vjust=.5))

但这也不起作用.有人可以告诉我我在做什么错吗?

But this is also not working. Could someone please show me what I am doing wrong?

谢谢

注意:如果有两列,是否仍然可以使用聚合函数?

Note: if there are two columns, is it still possible to use the aggregate function?

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") property_damages_in_dollars <- rnorm(731,100,10) other_damages_in_dollars <- rnorm(731,10,10) final_data <- data.frame(date_decision_made, other_damages_in_dollars, property_damages_in_dollars) y.mon<-aggregate(property_damages_in_dollars, other_damages_in_dollars ~format(as.Date(date_decision_made), format="%Y/%m"),data=final_data, FUN=sum)

推荐答案

一种方法可以将年份添加到星期,如下所示:

One way can be adding the year to the week like this:

library(ggplot2) #Code 1 #Data y.mon<-aggregate(property_damages_in_dollars~format(as.Date(date_decision_made), format="%W-%y"),data=final_data, FUN=sum) y.mon$week = y.mon$`format(as.Date(date_decision_made), format = "%W-%y")` #Plot ggplot(y.mon, aes(x = week, y=property_damages_in_dollars))+ geom_line(aes(group=1))+ scale_x_discrete(guide = guide_axis(n.dodge=2))+ theme(axis.text.x = element_text(angle = 45))

输出:

如果您感到好奇,我将使用 scale_x_date()并直接针对日期进行汇总:

Just if you feel curious, I will leave this here using scale_x_date() and aggregating directly against date:

#Code 2 y.mon<-aggregate(property_damages_in_dollars~as.Date(date_decision_made),data=final_data, FUN=sum) y.mon$week = y.mon$`as.Date(date_decision_made)` #Plot ggplot(y.mon, aes(x = week, y=property_damages_in_dollars)) + geom_line(aes(group=1))+ scale_x_date(date_labels = '%Y-%W',breaks = '8 weeks')

输出:

更多推荐

R:将日期从每天转换为每周,然后进行绘制

本文发布于:2023-10-27 04:16:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!