本文介绍了每个时间序列的多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据框,其中每个用户有288个观察值:
I have the following dataframe where each user has 288 observations:
User5 User8 User10
2015-01-01 00:00:00 12.3 10.3 17.5
2015-01-01 00:30:00 20.1 12.7 20.9
2015-01-01 01:00:00 12.8 9.2 17.8
2015-01-01 01:30:00 11.5 6.9 12.5
2015-01-01 02:00:00 12.2 9.2 7.5
2015-01-01 02:30:00 9.2 14.2 9.0
.................... .... .... ....
2015-01-01 23:30:00 11.2 10.7 16.8
如何制作每个时间序列具有多个图的图?
How can I make a graph with multiple graphs of each time series?
推荐答案
另一个选择是将宽格式转换为长格式,然后在同一图形中绘制所有内容.以下是使用@G发布的DF
的代码.格洛腾迪克
Another option is to convert from wide to long format then plot everything in the same graph. Below is the code that use the DF
posted by @G. Grothendieck
library(tidyverse)
library(scales)
# Convert Time from factor to Date/Time
DF$Time <- as.POSIXct(DF$Time)
# Convert from wide to long format (`tidyr::gather`)
df_long <- DF %>% gather(key = "user", value = "value", -Time)
# Plot all together, color based on User
# We use pretty_breaks() from scales package for automatic Date/Time labeling
ggplot(df_long, aes(Time, value, group = user, color = user)) +
geom_line() +
scale_x_datetime(breaks = pretty_breaks()) +
theme_bw()
要在单独的面板中绘制每个用户,请使用facet_grid
to plot each user in a separated panel, use facet_grid
ggplot(df_long, aes(Time, value, group = user, color = user)) +
geom_line() +
scale_x_datetime(breaks = pretty_breaks()) +
theme_bw() +
facet_grid(user ~ .)
这篇关于每个时间序列的多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!