R: convert dates from daily to weekly and plotting them

五迷三道 提交于 2021-01-20 12:26:46

问题


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))

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?

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?

Thanks

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)

回答1:


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))

Output:

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')

Output:



来源:https://stackoverflow.com/questions/65162036/r-convert-dates-from-daily-to-weekly-and-plotting-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!