R/multi_plot.R

Defines functions multi_plot

Documented in multi_plot

#' An implementation of matplot with nice coloring and automatic legend generation
#'
#' @param data A data.frame or matrix we wish to plot.
#' @param plot_columns A numeric vector of column indexes we wish to plot. Defaults to NULL in which case all columns are plotted.
#' @param legend_location Defaults to "bottomleft" but can be changed to any of "left", "topleft", "top", "topright", "right", "bottomright", "bottom".
#' @param xlabel The label we wish to give the X axis. Defaults to no label.
#' @param ylabel The label we wish to give the Y axis. Defaults to no label.
#' @param category The label we wish to give to the legend box.
#' @param output_pdf Defaults to FALSE. If TRUE, then the plot is output to a pdf with pdf_name to the current working directory.
#' @param pdf_name The name we wish to give out pdf. No .pdf extension is required as it is automatically appended.
#' @param height The height of the optional .pdf file generated by this function. Defaults to 5 (inches).
#' @param width The width of the optional .pdf file generated by this function. Defaults to 8 (inches).
#' @param connect_with_lines Logical indicating whether dots should be connected with a line, defaults to FALSE.
#' @param order_by_col Defaults to NULL. If not NULL, then a column index (must be in plot_columns) may be specified. The values of this column are used to sort X values in decreasing order in the resulting plot.
#' @param normalize Defaults to FALSE, if TRUE, then each column will be normalized by dividing it by its maximum absolute value.
#' @return A plot.
#' @export
multi_plot <- function(data,
                       plot_columns = NULL,
                       legend_location = "bottomleft",
                       xlabel = "",
                       ylabel = "",
                       category = "",
                       output_pdf = FALSE,
                       pdf_name = NULL,
                       height = 5,
                       width = 8,
                       connect_with_lines = FALSE,
                       order_by_col = NULL,
                       normalize = FALSE){

    UMASS_BLUE <- rgb(51,51,153,195,maxColorValue = 255)
    UMASS_RED <- rgb(153,0,51,195,maxColorValue = 255)
    UMASS_GREEN <- rgb(0,102,102,195,maxColorValue = 255)
    UMASS_YELLOW <- rgb(255,255,102,255,maxColorValue = 255)
    UMASS_ORANGE <- rgb(255,204,51,195,maxColorValue = 255)
    UMASS_PURPLE <- rgb(65,39,59,195,maxColorValue = 255)
    UMASS_BROWN <- rgb(148,121,93,195,maxColorValue = 255)

    # if we are ordering observations by a column
    if(!is.null(order_by_col)){
        ordering <- order(data[,order_by_col],decreasing = T)
        data <- data[ordering,]
    }

    #normalizes each row of a data frame
    normalize_values <- function(data,
                                 columns){
        for(i in columns){
            if(max(abs(data[,i]),na.rm = T) > 1){
                data[,i] <- data[,i]/max(abs(data[,i]),na.rm = T)
            }
        }
        return(data)
    }

    if(normalize){
        data <- normalize_values(data,plot_columns)
    }

    # defaults to plotting all columns
    if(is.null(plot_columns)){
        plot_columns <- 1:ncol(data)
    }

    colors <- c(UMASS_BLUE,UMASS_RED, UMASS_GREEN, UMASS_ORANGE,UMASS_PURPLE, UMASS_YELLOW, UMASS_BROWN)
    plot_colors <- NULL
    index <- 1
    for(i in 1:length(plot_columns)){
        plot_colors <- c(plot_colors, colors[index])
        index <- index + 1
        if(index == 6){
            index <- 1
        }
    }

    if(output_pdf){
        pdf(file = paste(pdf_name,".pdf",sep = ""),height=height, width=width, family="Times", pointsize=13.5)
        par(las=2,mar=c(6,5,.5,.5),cex.lab=.5,xpd=TRUE)

        if(connect_with_lines){
            matplot(data[,plot_columns],
                 ylim=c(min(data[,plot_columns],0),max(data[,plot_columns],1)),
                 lwd=1,
                 col = "grey80",
                 type="l",
                 xaxt= "n",
                 xlab=xlabel,
                 ylab=ylabel,
                 main = "",
                 cex.axis = 0.5)
        }else{
            matplot(data[,plot_columns],
                 ylim=c(min(data[,plot_columns],0),max(data[,plot_columns],1)),
                 lwd=-1,
                 col = "white",
                 type="l",
                 xaxt= "n",
                 xlab=xlabel,
                 ylab=ylabel,
                 main = "",
                 cex.axis = 0.5)
        }

        for(i in 1:nrow(data)){
            segments(x0=i,
                     y0=min(data[,plot_columns],0),
                     x1=i,y1=max(data[,plot_columns],1),
                     lwd=0.5,
                     col="grey80",
                     lty=3)
        }

        col_names <- NULL
        col_colors <- NULL
        col_shapes <- NULL
        for(i in 1:length(plot_columns)){
            points(data[,plot_columns[i]] ~ jitter(1:nrow(data),0.3),
                   pch=14+i,
                   col=plot_colors[i])
            #bg=plot_colors[i])
            col_names <- c(col_names,colnames(data)[plot_columns[i]])
            col_colors <- c(col_colors,plot_colors[i])
            col_shapes <- c(col_shapes,14+i)
        }
        axis(1, col='black', at=1:nrow(data), labels=rownames(data),lwd= 1, cex.axis = 0.5)

        legend(legend_location,
               legend=col_names,
               pch=col_shapes ,
               col = col_colors,
               title=category,
               cex = 0.5)
        dev.off()
    }else{
        par(las=2,mar=c(6,5,.5,.5),cex.lab=.5,xpd=TRUE)

        if(connect_with_lines){
            matplot(data[,plot_columns],
                 ylim=c(min(data[,plot_columns],0),max(data[,plot_columns],1)),
                 lwd=1,
                 col = "grey80",
                 type="l",
                 xaxt= "n",
                 xlab=xlabel,
                 ylab=ylabel,
                 main = "",
                 cex.axis = 0.5)
        }else{
            matplot(data[,plot_columns],
                 ylim=c(min(data[,plot_columns],0),max(data[,plot_columns],1)),
                 lwd=-1,
                 col = "white",
                 type="l",
                 xaxt= "n",
                 xlab=xlabel,
                 ylab=ylabel,
                 main = "",
                 cex.axis = 0.5)
        }
        for(i in 1:nrow(data)){
            segments(x0=i,y0=min(data[,plot_columns],0),x1=i,y1=max(data[,plot_columns],1),lwd=0.5,col="grey80",lty=3)
        }

        col_names <- NULL
        col_colors <- NULL
        col_shapes <- NULL
        for(i in 1:length(plot_columns)){
            points(data[,plot_columns[i]],
                   pch=14+i,
                   col=plot_colors[i])
            #bg=plot_colors[i])
            col_names <- c(col_names,colnames(data)[plot_columns[i]])
            col_colors <- c(col_colors,plot_colors[i])
            col_shapes <- c(col_shapes,14+i)
        }
        axis(1, col='black', at=1:nrow(data), labels=rownames(data),
             lwd= 1, cex.axis = 0.5)

        legend(legend_location,
               legend=col_names,
               pch=col_shapes ,
               col = col_colors,
               title=category,
               cex = 0.5)
    }
}
matthewjdenny/SpeedReader documentation built on March 25, 2020, 5:32 p.m.