# MvA plotting for microarray data # Copyright 2003 By Don Armstrong # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA. # $Id: mva_plotting.R,v 1.5 2006/06/10 04:14:45 don Exp $ library("grid") library("ggplot2") # mva.plot # Plots a pairwise M versus A plot with a loess fit line ##' Plots a pairwise M versus A plot with a loess fit line ##' ##' .. content for \details{} .. ##' @title mva.plot.pair ##' @param array Array of data to plot ##' @param title Title of plot (Default: FALSE; no title) ##' @param small.positive replace numbers less than 0 with this ##' @author Don Armstrong \email{don@@donarmstrong.com} mva.plot.pair <- function(array,title=FALSE,small.positive=0.1){ size <- ncol(array) npair <- size*(size-1)/2 height <- min(round((npair*3/4)^0.5),3) width <- min(ceiling(npair/height),3) def.par <- par(no.readonly = TRUE) mva.layout <- grid.layout(ncol=width,nrow=height) pushViewport(viewport(layout=mva.layout)) ## we could also use combn here instead num.plot <- 0 for(a in 1:(size-1)){ for(b in (a+1):size){ num.plot <- num.plot + 1 group.1 <- array[,a] group.2 <- array[,b] group.keep <- is.finite(group.1) & is.finite(group.2) group.1 <- group.1[group.keep] group.2 <- group.2[group.keep] group.min <- min(c(group.1,group.2)) if (group.min <= 0) { group.1 <- group.1 - group.min + small.positive group.2 <- group.2 - group.min + small.positive } A <- 0.5*log2(group.1*group.2) M <- log2(group.1/group.2) temp.frame <- na.omit(data.frame(cbind(A,M))) if (num.plot > 1 && floor((num.plot -1)/ width) %% height == 0 && (num.plot-1) %% width == 0) { popViewport(1) grid.text(title,x=unit(0.0,"npc")+unit(1,"lines"),y=unit(0.5,"npc"),just="center",rot=90) grid.newpage() pushViewport(viewport(layout=mva.layout)) } ## print(floor((num.plot -1)/ width) %% height+1) ## print((num.plot - 1)%% width+1) print(ggplot(temp.frame,aes(y=M,x=A))+ geom_hex()+ stat_smooth(method="gam")+ ggtitle(paste0(colnames(array)[a],"\nvs\n",colnames(array)[b]))+ theme(legend.position="none"), vp=viewport(layout.pos.row=floor((num.plot -1)/ width) %% height+1, layout.pos.col=((num.plot - 1)%% width+1))) } } popViewport() }