0

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).

p.s. 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.

1 Answer 1

1

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: enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help! But this is not exactly what I'm looking for. I don't want the mean for each day, but the mean temp of all the individuals in the group, for each time point (ToD). So to take the mean Temp value of all IDs in Cond AA, on Day 1, ToD 18.00, then at 18.01, and so on. (See my edit in the original post).
Then you just need to add ToD to the group_by statement, so that it is summarise(group_by(df, Cond, Date, ToD), t = mean(Temp)). This will give mean temperature of all data points for each condition, date, and time of day. Then you can use something like facet_grid(Cond ~ .) to get a separate graph for each condition.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.