【发布时间】:2021-03-25 02:43:35
【问题描述】:
我需要可视化并比较两个同样长的销售期的差异。 2018/2019 和 2019/2020。两个时期都从第 44 周开始,到次年的第 36 周结束。如果我创建一个图表,两个周期都是连续的并且排列整齐。如果我只使用周数,值会被排序为连续的,图表没有意义。你能想出一个解决办法吗?
谢谢
数据:
set.seed(1)
df1 <- data.frame(sells = runif(44),
week = c(44:52,1:35),
YW = yearweek(seq(as.Date("2018-11-01"), as.Date("2019-08-31"), by = "1 week")),
period = "18/19")
df2 <- data.frame(sells = runif(44),
week = c(44:52,1:35),
YW = yearweek(seq(as.Date("2019-11-01"), as.Date("2020-08-31"), by = "1 week")),
period = "19/20")
# Yearweek on x axis, when both period are separated
ggplot(df1, aes(YW, sells)) +
geom_line(aes(color="Period 18/19")) +
geom_line(data=df2, aes(color="Period 19/20")) +
labs(color="Legend text")
# week on x axis when weeks are like continuum and not splited by year
ggplot(df1, aes(week, sells)) +
geom_line(aes(color="Period 18/19")) +
geom_line(data=df2, aes(color="Period 19/20")) +
labs(color="Legend text")
【问题讨论】: