R/mgsplot.gradient_box.R

Defines functions mgsplot.gradient_box

Documented in mgsplot.gradient_box

#' This function creates boxplots
#'
#' @param data: dataframe 
#' @return ggplot object
#'
#' @import ggplot2
#' @import scales
#' 
#' @export mgsplot.gradient_box
#########################################################################

mgsplot.gradient_box <- function(data,
                                 dist,
                                 conc,
                                 bin=NULL){  
  
  #### START: Check inputs & set up .local variables  
  # Look to see what data was passed in to the function
  if (is.numeric(dist) & is.numeric(conc)){
    if (missing(bin)){
      # assume that we've been given vectors of the speed and concentration vectors
      data <- data.frame(dist = dist,
                         conc = conc)
      dist = "dist"
      conc = "conc"
    } else{
      # assume that we've been given vectors of the speed and concentration vectors
      data <- data.frame(dist = dist,
                         conc = conc,
                         bin = bin)
      dist = "dist"
      conc = "conc"
      bin = "bin"
      nc = ceiling(length(unique(data$bin))/3)
    }
  
  } else if (exists("data")){
    # Assume that we've been given a data frame, and the name of the dist 
    # and conc columns. This is the format we want for later use.    
  }
  
  # Tidy up input data ----
  n.in <- NROW(data)
  dnu <- (is.na(data[[dist]]) | is.na(data[[conc]]))
  data[[dist]][dnu] <- NA
  data[[conc]][dnu] <- NA
  #### END: Check inputs & set up .local variables 

  p.gradient_boxplot <- ggplot(data,aes(x=dist,y=conc,group=dist))+
  stat_boxplot(outlier.shape=1, alpha=0,outlier.colour="grey50")+
  theme_bw(base_size=12, base_family = "Helvetica")+
  scale_x_continuous(name="Distance from center of roadway [m]",breaks=unique(data$dist))+
  theme(axis.text.x = element_text(angle=45))+
  ylab("Concentration")+
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),labels = trans_format("log10", math_format(10^.x)))+
  stat_summary(fun.y=mean, geom="line", aes(group=1))  + 
  stat_summary(fun.y=mean, geom="point")+
  geom_vline(xintercept = 0,linetype="dashed")
  
  if (!missing(bin)){
    p.gradient_boxplot <- p.gradient_boxplot+facet_wrap(~bin,ncol = nc)
  }

# return the handle to the plot
return(p.gradient_boxplot)
} #end function
michellegrace/mgs.dispersion documentation built on May 22, 2019, 9:55 p.m.