]> git.donarmstrong.com Git - loess_normalization.git/blob - R/mva_plotting.R
add initial version of loessnorm
[loess_normalization.git] / R / mva_plotting.R
1 # MvA plotting for microarray data
2
3
4 # Copyright 2003 By Don Armstrong
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 # 02111-1307, USA.
20
21 # $Id: mva_plotting.R,v 1.5 2006/06/10 04:14:45 don Exp $
22
23
24 library("grid")
25 library("ggplot2")
26
27 # mva.plot
28 # Plots a pairwise M versus A plot with a loess fit line
29 ##' Plots a pairwise M versus A plot with a loess fit line
30 ##'
31 ##' .. content for \details{} ..
32 ##' @title mva.plot.pair
33 ##' @param array Array of data to plot
34 ##' @param title Title of plot (Default: FALSE; no title)
35 ##' @param small.positive replace numbers less than 0 with this
36 ##' @author Don Armstrong \email{don@@donarmstrong.com}
37 mva.plot.pair <- function(array,title=FALSE,small.positive=0.1){
38   size <- ncol(array)
39   npair <- size*(size-1)/2
40   height <- min(round((npair*3/4)^0.5),3)
41   width <- min(ceiling(npair/height),3)
42   def.par <- par(no.readonly = TRUE)
43   mva.layout <- grid.layout(ncol=width,nrow=height)
44   pushViewport(viewport(layout=mva.layout))
45   ## we could also use combn here instead
46   num.plot <- 0
47   for(a in 1:(size-1)){
48       for(b in (a+1):size){
49           num.plot <- num.plot + 1
50           group.1 <- array[,a]
51           group.2 <- array[,b]
52           group.keep <- is.finite(group.1) & is.finite(group.2)
53           group.1 <- group.1[group.keep]
54           group.2 <- group.2[group.keep]
55           group.min <- min(c(group.1,group.2))
56           if (group.min <= 0) {
57               group.1 <- group.1 - group.min + small.positive
58               group.2 <- group.2 - group.min + small.positive
59           }
60           A <- 0.5*log2(group.1*group.2)
61           M <- log2(group.1/group.2)
62           temp.frame <- na.omit(data.frame(cbind(A,M)))
63           if (num.plot > 1 &&
64               floor((num.plot -1)/ width) %% height == 0 &&
65               (num.plot-1) %% width == 0) {
66               popViewport(1)
67               grid.text(title,x=unit(0.0,"npc")+unit(1,"lines"),y=unit(0.5,"npc"),just="center",rot=90)
68               grid.newpage()
69               pushViewport(viewport(layout=mva.layout))
70           }
71           ## print(floor((num.plot -1)/ width) %% height+1)
72           ## print((num.plot - 1)%% width+1)
73           print(ggplot(temp.frame,aes(y=M,x=A))+
74                 geom_hex()+
75                 stat_smooth(method="gam")+
76                 ggtitle(paste0(colnames(array)[a],"\nvs\n",colnames(array)[b]))+
77                 theme(legend.position="none"),
78                 vp=viewport(layout.pos.row=floor((num.plot -1)/ width) %% height+1, layout.pos.col=((num.plot - 1)%% width+1)))
79                 
80       }
81   }
82   popViewport()
83 }