Home > Software engineering >  Trying to plot a time course, 2 factors, 1 response variable with standard error bars
Trying to plot a time course, 2 factors, 1 response variable with standard error bars

Time:07-18

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.

I've sort of done it with 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) 

or

df <- unite(df,col='Combined Factors',c(Factor1,Factor2),sep='-',remove=FALSE)
    df %>% 
      group_by(Week,`Combined Factors`) %>% 
      mutate(avg_Response=mean(Response),se_Response=sd(Response)/sqrt(4)) %>% 
      ggplot(aes(Week, Response,group=`Combined Factors`,color=`Combined Factors`)) 
      geom_smooth(se=FALSE) %>% 
      {.} -> Line_plot_Response 
       

enter image description here

  • Related