From 37f094272fc3d8147ebca16fc0db72e390cbad55 Mon Sep 17 00:00:00 2001 From: Daria Zenkova Date: Thu, 1 Sep 2016 16:07:33 +0300 Subject: [PATCH] added support of size and colour in ggplot2 --- NAMESPACE | 1 + R/createES.R | 12 ++++++++++++ R/pcaPlot.R | 33 ++++++++++++++++++++++++++------- morpheusR.Rproj | 5 +++++ 4 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 R/createES.R diff --git a/NAMESPACE b/NAMESPACE index 5b9879b..328b0b5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,4 @@ # Generated by roxygen2: do not edit by hand export(pcaPlot) +export(createES) \ No newline at end of file diff --git a/R/createES.R b/R/createES.R new file mode 100644 index 0000000..1a323f8 --- /dev/null +++ b/R/createES.R @@ -0,0 +1,12 @@ +createES <- function(data, pData, labelDescription, colNames, rowNames) { + exprs <- data + colnames(exprs) <- colNames + truePData <- t(pData) + pd <- data.frame(truePData, row.names = colNames) + names(pd) <- labelDescription + require(Biobase) + phenoData <- AnnotatedDataFrame(pd) + featureData <- AnnotatedDataFrame(data.frame(rowNames)) + es <- ExpressionSet(assayData = exprs, phenoData=phenoData, featureData = featureData) + return(es) +} \ No newline at end of file diff --git a/R/pcaPlot.R b/R/pcaPlot.R index 7a33e62..13802f7 100644 --- a/R/pcaPlot.R +++ b/R/pcaPlot.R @@ -5,20 +5,39 @@ #' @examples #' pcaPlot(es.norm, 1, 2) + aes(color=time) #' @export -pcaPlot <- function(es, c1, c2) { +pcaPlot <- function(es, columns, c1, c2, size="", colour="") { stopifnot(require(ggplot2)) - pca <- prcomp(t(exprs(es))) - + stopifnot(require(Biobase)) + data <- t(exprs(es)[columns,]) + pca <- prcomp(~., data.frame(data)) explained <- (pca$sdev)^2 / sum(pca$sdev^2) xs <- sprintf("PC%s", seq_along(explained)) xlabs <- sprintf("%s (%.1f%%)", xs, explained * 100) - pp <- ggplot(data=cbind(as.data.frame(pca$x), pData(es))) - + pData <- pData(es)[!(rownames(pData(es)) %in% setdiff(rownames(pData(es)), rownames(pca$x))),] + pp <- ggplot(data=cbind(as.data.frame(pca$x), pData)) + if (size == "" && colour == "") { + aes <- aes(x=eval(parse(text=xs[c1])), + y=eval(parse(text=xs[c2]))) + } else if (size == "" && colour != "") { + c <- pData[[colour]]; + aes <- aes(x=eval(parse(text=xs[c1])), + y=eval(parse(text=xs[c2])), colour=c) + } else if (size != "" && colour == "") { + s <- pData[[size]]; + aes <- aes(x=eval(parse(text=xs[c1])), + y=eval(parse(text=xs[c2])), size=s) + } else { + s <- pData[[size]] + c <- pData[[colour]] + aes <- aes(x=eval(parse(text=xs[c1])), + y=eval(parse(text=xs[c2])), colour=c,size=s) + } + + pp + - geom_point(aes(x=eval(parse(text=xs[c1])), - y=eval(parse(text=xs[c2]))), size=3) + + geom_point(aes) + xlab(xlabs[c1]) + ylab(xlabs[c2]) } diff --git a/morpheusR.Rproj b/morpheusR.Rproj index d848a9f..cba1b6b 100644 --- a/morpheusR.Rproj +++ b/morpheusR.Rproj @@ -5,8 +5,13 @@ SaveWorkspace: No AlwaysSaveHistory: Default EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 Encoding: UTF-8 +RnwWeave: Sweave +LaTeX: pdfLaTeX + AutoAppendNewline: Yes StripTrailingWhitespace: Yes -- GitLab