diff --git a/R/pcaPlot.R b/R/pcaPlot.R index b579087c99fc18d542bb27e4b403559757a2f5ee..a3fd4d195df6c84b1bd9946b3948a330749fa8f8 100644 --- a/R/pcaPlot.R +++ b/R/pcaPlot.R @@ -5,30 +5,44 @@ #' @examples #' pcaPlot(es.norm, 1, 2) + aes(color=time) #' @export -pcaPlot <- function(es, columns, c1, c2, size="", colour="") { +pcaPlot <- function(es, columns=c(), rows=c(), c1, c2, size="", colour="") { + с1 <- as.numeric(c1) + c2 <- as.numeric(c2) stopifnot(require(ggplot2)) stopifnot(require(Biobase)) + if (is.na(size)) { size = "" } if (is.na(colour)) { colour = "" } - data <- t(exprs(es)[columns,]) - pca <- prcomp(~., data.frame(data)) + if (is.null(rows)) { + rows <- 1:nrow(exprs(es)) + } + if (is.null(columns)) { + columns <- 1:ncol(exprs(es)) + } + + rows <- as.numeric(rows) + columns <- as.numeric(columns) + + data <- t(exprs(es)[rows,columns]) + pca <- prcomp(data) explained <- (pca$sdev)^2 / sum(pca$sdev^2) - print(size) - print(colour) xs <- sprintf("PC%s", seq_along(explained)) xlabs <- sprintf("%s (%.1f%%)", xs, explained * 100) pData <- pData(es)[!(rownames(pData(es)) %in% setdiff(rownames(pData(es)), rownames(pca$x))),] + + if (size != "") { + pData[[size]] <- as.numeric(pData[[size]]) + } + pp <- ggplot(data=cbind(as.data.frame(pca$x), pData)) if (size != "" && colour != "") { - pData[[size]] <- as.numeric(pData[[size]]) - aes <- aes_string(x=xs[c1], y=xs[c2], colour=colour, size=size) @@ -36,7 +50,6 @@ pcaPlot <- function(es, columns, c1, c2, size="", colour="") { aes <- aes_string(x=xs[c1], y=xs[c2], colour=colour) } else if (size != "") { - pData[[size]] <- as.numeric(pData[[size]]) aes <- aes_string(x=xs[c1], y=xs[c2], size=size) } else { @@ -44,7 +57,6 @@ pcaPlot <- function(es, columns, c1, c2, size="", colour="") { y=xs[c2]) } - pp + geom_point(aes) + xlab(xlabs[c1]) + ylab(xlabs[c2])