R/plot_dauer_SEs.R

#' plot_dauer_SEs
#'
#' Function generates boxplots for dauer data along with standard error generate from a glm fit.
#' @param df input dataset. Requires a "genotype" column. Response needs to be "pct" for
#' dauer plots. Also requires a "mixed" dataset, generated by either MM_CIs.R, Mm_CI_trans_Tukey.R or MM_CI_trans_dunnett.R
#' @param title title for the plot
#' @param plot.contrasts list of pairwise comparisons to plot. Usually generated by selecting a list 
#' of comparisons using tukey_contrasts or dunnett_contrasts functions which use the lsmeans
#' package, ie plot.contrasts = c("",contrasts$prange[1:2]) for 2
#' comparisons to a control.
#' @param plot.contrasts.2 list of secondary comparisons (ie rescue lines). These will be colored red and offset in the
#' y direction indicated by offset.
#' @param ypos y position of p value for plot.contrasts on the plot response scale
#' @export
#' @examples plot_dauer_SEs(df, title = "dauer plot", plot.contrasts = plot.contrasts, ypos =  1.075)
plot_dauer_SEs<-function (df, title, plot.contrasts, plot.contrasts.2, ypos) {
  p <- ggplot(df, aes(x=genotype, y=pct)) + geom_boxplot(width=0.3, outlier.shape=NA) + geom_point(cex=1,alpha = 0.5) + 
    labs(title = title,
         y = "proportion dauer",
         x = "genotype"
    ) +
    theme_my + 
    geom_point(data=mixed, aes(x=x.pos, y=mean), colour="blue") +
    geom_errorbar(data=mixed, aes(x=x.pos,y=mean, ymin=mean-SE, ymax=mean+SE),
                  width=.1,colour ="blue") + 
    scale_y_continuous(breaks=c(0,0.25,0.5,0.75, 1.0)) +
    stat_summary(aes(x=genotype, y=ypos), geom="text", label=plot.contrasts, size=3, angle =45)
  if(missing(plot.contrasts.2)) {
    p
  } else {
    p + stat_summary(aes(x=genotype, y=ypos-0.075), geom="text", label=plot.contrasts.2, size=3, colour="red") +  + stat_summary(aes(x=as.numeric(as.factor(genotype)) + 0.3, y=min),
                                                                                                                                 fun.data = fun_length, geom = "text", size = 3)
  }
}
mikeod38/dauergut documentation built on May 30, 2019, 7:16 p.m.