简体   繁体   English

时间序列数据可视化

[英]Time-series data visualization

I have a pretty large data frame in R stored in long form. 我在R中有一个很大的数据框,以长格式存储。 It contains body temperature data collected from 40 different individuals, with 10 sec intervals, over 16 days. 它包含从40个不同的人收集的体温数据,每16秒间隔10秒。 Individuals have been exposed to conditions (cond1 and cond2). 个人已暴露于条件(cond1和cond2)。 It essentially looks like this: 基本上看起来像这样:

ID  Cond1  Cond2  Day  ToD  Temp
 1      A      B    1 18.0  37.1
 1      A      B    1 18.3  37.2
 1      A      B    2 18.6  37.5
 2      B      A    1 18.0  37.0
 2      B      A    1 18.3  36.9
 2      B      A    2 18.6  36.9
 3      A      A    1 18.0  36.8
 3      A      A    1 18.3  36.7
 3      A      A    2 18.6  36.7
...

I want to create four separate line plots for each combination of conditions(AB, BA, AA, BB) that shows mean temp over time (day 1-16). 我想为条件(AB,BA,AA,BB)的每种组合创建四个单独的折线图,以显示随时间(第1-16天)的平均温度。

ps ToD stands for time of day. ps ToD代表一天中的时间。 Not sure if I need to provide it in order to create the plot. 不知道是否需要提供它才能创建图。

So far I have tried to define the dataset as time series by doing 到目前为止,我已经尝试通过以下方式将数据集定义为时间序列:

ts <- ts(data=dataset$Temp, start=1, end=16, frequency=8640)
plot(ts)

This returns a plot of Temp, but I can't figure out how to define condition values for breaking up the data. 这将返回Temp的图,但是我无法弄清楚如何定义条件值来分解数据。

Edit: Essentially I want a plot that looks like this 1 , but one for each group separately, and using mean Temp values. 编辑:本质上我想要一个看起来像这样1的图 ,但每个组分别一个,并使用平均Temp值。 This plot is just for one individual in one condition, and I want one that shows the mean for all individuals in the same condition. 此图仅适用于处于一种情况的一个人,而我想要一个显示所有处于相同情况的人的均值。

You can use summarise and group_by to group the data by condition and then plot it. 您可以使用summarisegroup_by到GROUP BY条件的数据,然后绘制。 Is this what you're looking for? 这是您要找的东西吗?

library(dplyr)
## I created a dataframe df that looks like this:
  ID Cond1 Cond2 Day  ToD Temp
1  1     A     B   1 18.0 37.1
2  1     A     B   1 18.3 37.2
3  1     A     B   2 18.6 37.5
4  2     B     A   1 18.0 37.0
5  2     B     A   1 18.3 36.9
6  2     B     A   2 18.6 36.9
7  3     A     A   1 18.0 36.8
8  3     A     A   1 18.3 36.7
9  3     A     A   2 18.6 36.7
df$Cond <- paste0(df$Cond1, df$Cond2)
d <- summarise(group_by(df, Cond, Day), t = mean(Temp))
ggplot(d, aes(Day, t, color = Cond)) + geom_line()

which results in: 结果是: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM