R ggplot每日时间序列分为几个月和几年

我的数据是从2010年到2019年的每天时间序列。

我希望在一幅图中绘制折线图,​​其中包含所有年份一月份的所有日销售量(1-30)。然后是下一个2月的地块,依此类推。我画了一幅画以更好地了解我的问题。

如果我使用facet_wrap或facet_grid函数,则仅用于月份透视。我还每天尝试使用forecast::ggseansonplot(),但是输出消息是没有季节性。但是我只想绘制一个图,不进行分析。

插图:

R ggplot每日时间序列分为几个月和几年

library(ggplot2)
library(plyr)
library(readxl)

s <- read_excel("sales.xlsx")

s$date <- as.Date(s$date)
s$month <- as.Date(cut(s$date,breaks="month"))

base <- ggplot(s,aes(date,sales)) +
geom_line()
base + facet_wrap(~month,ncol=6)

感谢您的帮助

Steph

liulinawoaini 回答:R ggplot每日时间序列分为几个月和几年

我建议使用lubridate,将日期划分为几天,几个月和几年,然后以day在x轴上,以month作为面进行绘制:>

library(lubridate)
library(tidyverse)
set.seed(0)
s = tibble(
  date=ymd(20130101) + seq(1:(3*365)),sales = rnorm(3*365),day = day(date),month = month(date),year = as.factor(year(date))
)

ggplot(s,aes(day,sales,colour=year)) +
  geom_line() +
  facet_wrap(~month,ncol=6)

enter image description here

本文链接:https://www.f2er.com/3096732.html

大家都在问