我的时间序列从2015-01-01
开始到Sys.time()
我想制作一个情节,我将两者相互比较:
原因是产生一个总体情节,显示按小时和工作日平均的值,以便比较上一年和当前年份:
library(dplyr)
library(lubridate)
library(ggplot2)
# Some data
start.date <- as.POSIXct('2015-01-01')
end.date <- Sys.time()
tseq <- seq(start.date, end.date, by = "hours")
data <- sample(1:10,length(tseq),replace = T)
sales<- cumsum(rnorm(length(tseq), mean=data, sd=sqrt(data)))
df<- cbind.data.frame(tseq,sales)
head(df)
# spitting date to days in week
df$week_days<-as.numeric(format(df$tseq, "%w"))
# spitting date to days in hours
df$hours<-as.numeric(format(df$tseq, "%H"))
# subset from 2015-01-01 to Current timestamp - 365 days
my_interval<- Sys.time()-1 -dyears(1)
df_15 <- df %>% filter(tseq < my_interval)
my_plot15=aggregate(sales ~ + hours + week_days, data =df_15, FUN=mean)
ggplot(my_plot15, aes(hours, sales, group=week_days, col=week_days)) +
geom_line() +
geom_point()+
ylab("Avgvalue")+
xlab("Hours")+
ggtitle("Average sales by hour and month 2015") +
theme_bw()
# from 2016 to Current timestamp
# subset from 2015-01-01 to Current timestamp - 365 days
df_16 <- df %>% filter(tseq >= '2016-01-01')
my_plot16=aggregate(sales ~ + hours + week_days, data =df_16, FUN=mean)
ggplot(my_plot16, aes(hours, sales, group=week_days, col=week_days)) +
geom_line() +
geom_point()+
ylab("Avgvalue")+
xlab("Hours")+
ggtitle("Average sales by hour and month 2016") +
theme_bw()
如何将它们整理成一个图?
答案 0 :(得分:3)
如果您将数据合并到一个数据框中,则可以通过facet_wrap()
:
my_plot15$year <- 2015
my_plot16$year <- 2016
my_plot <- rbind(my_plot15, my_plot16)
ggplot(my_plot, aes(hours, sales, group=week_days, col=week_days)) +
geom_line() +
geom_point()+
facet_wrap(~year, ncol=1, scales = "free_y") +
ylab("Avgvalue")+
xlab("Hours")+
ggtitle("Average sales by hour and month 2016") +
theme_bw()
答案 1 :(得分:1)
给出数据的每小时性质,我建议使用雷达图。请注意,对于实际的连续时间数据,这应该看起来要好得多,因为从第23小时到0会看起来好多了。
大量借鉴here。
coord_radar <- function (theta = "x", start = 0, direction = 1)
{
theta <- match.arg(theta, c("x", "y"))
r <- if (theta == "x")
"y"
else "x"
ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start,
direction = sign(direction),
is_linear = function(coord) TRUE)
}
做一些数据准备:
# Get 2016 at the mean of 2015
my_plot16$sales <- my_plot16$sales - mean(my_plot16$sales) + mean(my_plot15$sales)
complete <- bind_rows('2015' = my_plot15, '2016' = my_plot16, .id = 'year')
complete$week_days <- factor(complete$week_days,
labels = c('M', 'Tu', 'W', 'Th', 'F', 'Sa', 'Su'))
简介:
ggplot(complete, aes(x = hours, y = sales, group = year, color = year)) +
geom_polygon(fill = NA, size = 2, show.legend = FALSE) +
geom_line(size = 2) +
facet_wrap(~week_days) +
coord_radar() +
theme_minimal()