简体   繁体   中英

Time-series data visualization

I have a pretty large data frame in R stored in long form. It contains body temperature data collected from 40 different individuals, with 10 sec intervals, over 16 days. Individuals have been exposed to conditions (cond1 and 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).

ps ToD stands for time of day. 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.

Edit: Essentially I want a plot that looks like this 1 , but one for each group separately, and using mean Temp values. 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. 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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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