R/plotTimeCourse.R

Defines functions plotTimeCourse

Documented in plotTimeCourse

#' Plot a time course data
#' 
#' Make sure y, st.err.mean are not of the same length with 1-2-1 relationship. 
#' Also, make sure there is 1-2-1 reationship between 'col' and 'y'. 
#' If lists are supplied than, within each list member, make sure 'y', 'st.err.mean' are of the same length with 1-2-1 relationship. 
#' 
#' Note that x-axis ticks are going to be named after the elements of the vector(s) names. 
#' 
#' @param y A named numeric vector of values to be plotted on the Y-axis OR a list of these vectors. Vector names will be plotted on the X-axis. List names will be used when plotting legend (if addLegend is TRUE). Note that x-axis ticks are going to be named after the elements of the vector(s) names. 
#' @param st.err.mean A numeric vector of standard error of the mean (or other measure of variability) OR a list of these vectors. Defults to `rep(0,length(y))`.
#' @param length A numeric of lengths of the edges of the arrow head (in inches). Defults to 0.1.
#' @param col A chracter of the color of line connecting the data points across the time course. If list supplied ensure there is a color assigned to each case. Defults to "red".
#' @param main A character of the graph title. Defults to "".
#' @param xlab A character of X axis label. Defults to "".
#' @param ylab A character of Y axis label. Defults to "".
#' @param type A character of type of plot should be drawn. See ?plot for types.
#' @param lty A numeric of line type. 
#' @param lwd A numeric of line width.
#' @param addLegend A boolean indicating whether to plot legend. Defults to TRUE.
#' @param legendPosition A character of logend position. One of "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center". Defults to "topright".
#' @author Alexander Kheirallah ([email protected])
#' @export


plotTimeCourse <- function(y,st.err.mean=rep(0,length(y)),length=0.1,col="red",main="",xlab="",ylab="",type="l",lty=1,lwd=2,addLegend=T,legendPosition="topright"){
  if(!class(y) == "list"){
    plot(y,
         lty=lty,
         lwd=lwd,
         type=type,
         xaxt="n",
         xlab=xlab,
         ylab=ylab,
         main = main,
         ylim = c(min(y)-max(st.err.mean),
                  max(y)+max(st.err.mean)),
         col = col)
    error.bar(x=1:length(y),y = y, upper = st.err.mean,length = length)
    axis(1,at=1:length(y),labels=names(y))
    if(addLegend){
      legend(legendPosition,
             names(y),
             fill=col,
             bty = "n")
    }
  }
  else{
    maximum.ValueOfAll = max(unlist(lapply(y,FUN = max)))
    minimum.ValueOfAll = min(unlist(lapply(y,FUN = min)))
    
    maximum.st.err.meanOfAll = max(unlist(lapply(st.err.mean,FUN=max)))
    plot(y[[1]],
         lwd=lwd,
         lty=lty,
         type=type,
         xaxt="n",
         xlab=xlab,
         ylab=ylab,
         main = main,
         ylim = c(minimum.ValueOfAll-maximum.st.err.meanOfAll,
                  maximum.ValueOfAll+maximum.st.err.meanOfAll),
         col = col)
    error.bar(x=1:length(y[[1]]),y = y[[1]], upper = st.err.mean[[1]], length = length)
    axis(1,at=1:length(y[[1]]),labels=names(y[[1]]))
    for(i in 2:length(y)){
      lines(y[[i]],
            col = col[i],
            type = type,
            lty = lty,
            lwd=lwd)
      error.bar(x=1:length(y[[i]]),y = y[[i]], upper = st.err.mean[[i]], length = length)
    }
    if(addLegend){
      legend(legendPosition,
             names(y),
             fill=col,
             bty = "n")
    }
  }
} 
msxakk89/dat documentation built on March 25, 2020, 3:18 p.m.