Home > database >  Plotting a time course of a response variable (that has 2 factors)
Plotting a time course of a response variable (that has 2 factors)

Time:07-08

As title states, I'm trying to plot a time course of a response variable (that has 2 factors), working environment based in Rstudio. I'm working off a data frame that's already in long format. Something like:

|Week| Factor1 | Factor2| Response|
 
  1     Sunny     High      2.0
  1     Sunny     High      3.5
  1     Rainy     Low       2.5
  1     Rainy     Low       1.5
  2     Sunny     High      42.5
  2     Sunny     High      435
  2     Rainy     Low       44.5
  2     Rainy     Low       42.5
  3     Sunny     High      80.5
  3     Sunny     High      89.5
  3     Rainy     Low       88.5
  3     Rainy     Low       87.5

I would like to do a time course with this data frame, but haven't had much success as I cannot figure out how to make ggplot2 plot the Response line as a variable responding to the combination of Factors.

EDIT* I've tried the solution provided below but the result was this... It seems like its not averaging out the data? at each x-axis plotting point? enter image description here

CodePudding user response:

Ok, so the answer after the hints from @Park would be to use geom_smooth(se=FALSE) instead of geom_line. Doing so with the se=FALSE argument removes the confidence interval bounds.

library(dplyr)
library(ggplot2)
library(tidyr)
df %>%
  unite(Factor, c(Factor1, Factor2)) %>%
  ggplot(aes(Week, Response, group = Factor, color = Factor))  
  geom_smooth(se=FALSE)

now to figure out how to add in error bars at each y-axis point

  • Related