R/ezline_3iv.R

Defines functions ezline_3iv

Documented in ezline_3iv

#' Visualisation of a three-way ANOVA using line charts.
#'
#' This function can be used to visualise a factorial experiment with
#' three independent variables. It displays the cell means and
#' corresponding standard errors in a line chart.
#'
#' @param afex_fit a fitted afex object.
#' @param factors a character vector specifying the three independent variables.
#' @return a ggplot object.
#' @examples
#' library(afexplot)
#' data(obk.long)
#' fit = aov_ez(dv="value", id="id", between=c("gender","treatment"), within="phase",data=obk.long)
#' ezline_3iv(afex_fit=fit, factors=c("gender","phase","treatment"))


ezline_3iv = function(afex_fit, factors,legend.pos=c(0.9,0.9)){
  dv = strsplit(attr(afex_fit$anova_table,"heading")[2],split=":")[[1]][2]
  ref <-lsmeans(afex_fit,specs = factors)
  df <- as.data.frame(summary(ref))
  pd <- position_dodge(0.1)
  p <- ggplot(df, aes_string(x=factors[1], y="lsmean",group=factors[2],colour=factors[2]))+
    geom_errorbar(aes(ymin=lsmean-SE, ymax=lsmean+SE), width=.1,position=pd) +
    geom_line(position=pd)+
    labs(y=dv)+
    scale_colour_grey()+
    geom_point(position=pd)+
    facet_wrap(reformulate(factors[3]))+
    theme_bw()+
    theme(legend.background = element_rect(colour = 'black'),
          legend.position = legend.pos,
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank())
  return(p)
}
mertensu/afexplot documentation built on June 21, 2017, 2:17 a.m.