R/plotTimeCourse.2.R

Defines functions plotTimeCourse.2

Documented in plotTimeCourse.2

#' Plot a time course data (version 2)
#' 
#' Make sure x, y, st.err.mean, col are of the same length with 1-2-1 relationship. 
#' 
#' @param time Date-type vector. X-axis will be labelled date-wise if label_Xaxis is TRUE. Should not have NAs.
#' @param y Numeric vector of values to be plotted on the Y-axis OR a list of these vectors for multi-line plots. May have NAs. Please ensure time vector is relevant for all y vectors if list supplied.
#' @param st.err.mean 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 Numeric of lengths of the edges of the arrow head (in inches). Defults to 0.1.
#' @param col Chracter of the color of line connecting the data points across the time course. If list supplied ensure there is Color assigned to each case. Defults to "red".
#' @param main Character of the graph title. Defults to "".
#' @param xlab Character of X axis label. Defults to "".
#' @param ylab Character of Y axis label. Defults to "".
#' @param type Character of type of plot should be drawn. See ?plot for types.
#' @param lty Numeric of line type. 
#' @param lwd Numeric of line width.
#' @param addLegend A boolean indicating whether to plot legend. Defults to TRUE.
#' @param legendPosition Character of logend position. One of "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center". Defults to "topright".
#' @param label_Xaxis Boolean. Should X-axis be labeled with time-type data?. Defults to TRUE.
#' @export


plotTimeCourse.2 <- function(time,y,st.err.mean,length=0.1,col="red",main="",xlab="",ylab="",type="l",lty=1,lwd=2,addLegend=T,legendPosition="topright",label_Xaxis=T){
  minTime = min(time)
  maxTime = max(time)
  if(!class(y) == "list"){
    
    min_y = min(y[!is.na(y)])
    max_y = max(y[!is.na(y)])
    
    max_st.err.mean = max((st.err.mean[!is.na(st.err.mean)]))
    
    if(label_Xaxis){
      plot(x=time[!is.na(y)],
           y=y[!is.na(y)],
           lty=lty,
           lwd=lwd,
           type=type,
           xlab=xlab,
           ylab=ylab,
           main = main,
           ylim = c(min_y-max_st.err.mean,
                    max_y+max_st.err.mean),
           xlim = c(minTime,maxTime),
           col = col)
      error.bar(x=time,y = y, upper = st.err.mean,length = length)
    }
    else{
      plot(x=time[!is.na(y)],
           y=y[!is.na(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),
           xlim = c(minTime,maxTime),
           col = col)
      error.bar(x=time,y = y, upper = st.err.mean,length = length)
    }
    
  }
  else{
    min_ofAll_y = min(unlist(lapply(y,FUN = function(x){
      x = x[!is.na(x)]
      min(x)
    })))
    max_ofAll_y = max(unlist(lapply(y,FUN = function(x){
      x = x[!is.na(x)]
      max(x)
    })))
    
    max_ofAll_st.err.mean = max(unlist(lapply(st.err.mean[!is.na(st.err.mean)],FUN=max)))

    if(label_Xaxis){
      plot(x=time[!is.na(y[[1]])],
           y=y[[1]][!is.na(y[[1]])],
           lty=lty,
           lwd=lwd,
           type=type,
           xlab=xlab,
           ylab=ylab,
           main = main,
           ylim = c(min_ofAll_y-max_ofAll_st.err.mean,
                    max_ofAll_y+max_ofAll_st.err.mean),
           xlim = c(minTime,maxTime),
           col = col)
      
      error.bar(x=time,y = y[[1]], upper = st.err.mean[[1]],length = length)
    }
    else{
      plot(x=time[!is.na(y[[1]])],
           y=y[[1]][!is.na(y[[1]])],
           lty=lty,
           lwd=lwd,
           type=type,
           xlab=xlab,
           ylab=ylab,
           main = main,
           ylim = c(min_y-max_ofAll_st.err.mean,
                    max_y+max_ofAll_st.err.mean),
           xlim = c(minTime,maxTime),
           xaxt="n",
           col = col)
      error.bar(x=1:length(y[[1]]),y = y[[1]], upper = st.err.mean[[1]],length = length)
    }
    for(i in 2:length(y)){
      lines(x = time[!is.na(y[[i]])],
            y = y[[i]][!is.na(y[[i]])],
            col = col[i],
            type = type,
            lty = lty,
            lwd=lwd)
      error.bar(x=time,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 Aug. 3, 2020, 6:39 p.m.