ggplot2时间段比较
ggplot2 comparation of time period
我需要可视化并比较两个同样长的销售期的差异。 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")
试试这个。您可以将周变量格式化为一个因素并保持所需的顺序。这里的代码:
library(ggplot2)
library(tsibble)
#Data
df1$week <- factor(df1$week,levels = unique(df1$week),ordered = T)
df2$week <- factor(df2$week,levels = unique(df2$week),ordered = T)
#Plot
ggplot(df1, aes(week, sells)) +
geom_line(aes(color="Period 18/19",group=1)) +
geom_line(data=df2, aes(color="Period 19/20",group=1)) +
labs(color="Legend text")
输出:
如果你想保持x轴为数字刻度,你可以这样做:
ggplot(df1, aes((week + 9) %% 52, sells)) +
geom_line(aes(color="Period 18/19")) +
geom_line(data=df2, aes(color="Period 19/20")) +
scale_x_continuous(breaks = 1:52,
labels = function(x) ifelse(x == 9, 52, (x - 9) %% 52),
name = "week") +
labs(color="Legend text")
另一种选择是刻面。这将需要将两组合并为一组,同时保留数据源。 (无论如何,这通常是一种更好的处理方式。)
(我没有tstibble
,所以我的YW
只有seq(...)
,没有yearweek
。应该翻译。)
ggplot(dplyr::bind_rows(tibble::lst(df1, df2), .id = "id"), aes(YW, sells)) +
geom_line(aes(color = id)) +
facet_wrap(id ~ ., scales = "free_x", ncol = 1)
代替 dplyr::bind_rows
,也可以使用 data.table::rbindlist(..., idcol="id")
或 do.call(rbind, ...)
,但对于后者,您需要在外部分配 id
。
还有一点要注意:x 轴的默认格式会掩盖数据的“年份”。如果这是 relevant/important(并且在其他地方不明显),则使用 ggplot2
的正常机制来强制标记,例如
... +
scale_x_date(labels = function(z) format(z, "%Y-%m"))
虽然没有 tibble::lst
可用时您不太可能执行此操作,但您可以将其替换为 list(df1=df1, df2=df2)
或类似内容。
我需要可视化并比较两个同样长的销售期的差异。 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")
试试这个。您可以将周变量格式化为一个因素并保持所需的顺序。这里的代码:
library(ggplot2)
library(tsibble)
#Data
df1$week <- factor(df1$week,levels = unique(df1$week),ordered = T)
df2$week <- factor(df2$week,levels = unique(df2$week),ordered = T)
#Plot
ggplot(df1, aes(week, sells)) +
geom_line(aes(color="Period 18/19",group=1)) +
geom_line(data=df2, aes(color="Period 19/20",group=1)) +
labs(color="Legend text")
输出:
如果你想保持x轴为数字刻度,你可以这样做:
ggplot(df1, aes((week + 9) %% 52, sells)) +
geom_line(aes(color="Period 18/19")) +
geom_line(data=df2, aes(color="Period 19/20")) +
scale_x_continuous(breaks = 1:52,
labels = function(x) ifelse(x == 9, 52, (x - 9) %% 52),
name = "week") +
labs(color="Legend text")
另一种选择是刻面。这将需要将两组合并为一组,同时保留数据源。 (无论如何,这通常是一种更好的处理方式。)
(我没有tstibble
,所以我的YW
只有seq(...)
,没有yearweek
。应该翻译。)
ggplot(dplyr::bind_rows(tibble::lst(df1, df2), .id = "id"), aes(YW, sells)) +
geom_line(aes(color = id)) +
facet_wrap(id ~ ., scales = "free_x", ncol = 1)
代替 dplyr::bind_rows
,也可以使用 data.table::rbindlist(..., idcol="id")
或 do.call(rbind, ...)
,但对于后者,您需要在外部分配 id
。
还有一点要注意:x 轴的默认格式会掩盖数据的“年份”。如果这是 relevant/important(并且在其他地方不明显),则使用 ggplot2
的正常机制来强制标记,例如
... +
scale_x_date(labels = function(z) format(z, "%Y-%m"))
虽然没有 tibble::lst
可用时您不太可能执行此操作,但您可以将其替换为 list(df1=df1, df2=df2)
或类似内容。