|
## Modified plotPCA from DESeq2 package. Shows the Names of the Samples (the first col of SampleTable), and uses ggrepel pkg to plot them conveniently. |
|
# @SA 10.02.2017 |
|
library(genefilter) |
|
library(ggplot2) |
|
library(ggrepel) |
|
|
|
plotPCA.san <- function (object, intgroup = "condition", ntop = 500, returnData = FALSE) |
|
{ |
|
rv <- rowVars(assay(object)) |
|
select <- order(rv, decreasing = TRUE)[seq_len(min(ntop, |
|
length(rv)))] |
|
pca <- prcomp(t(assay(object)[select, ])) |
|
percentVar <- pca$sdev^2/sum(pca$sdev^2) |
|
if (!all(intgroup %in% names(colData(object)))) { |
|
stop("the argument 'intgroup' should specify columns of colData(dds)") |
|
} |
|
intgroup.df <- as.data.frame(colData(object)[, intgroup, drop = FALSE]) |
|
group <- if (length(intgroup) > 1) { |
|
factor(apply(intgroup.df, 1, paste, collapse = " : ")) |
|
} |
|
else { |
|
colData(object)[[intgroup]] |
|
} |
|
d <- data.frame(PC1 = pca$x[, 1], PC2 = pca$x[, 2], group = group, |
|
intgroup.df, name = colData(rld)[,1]) |
|
if (returnData) { |
|
attr(d, "percentVar") <- percentVar[1:2] |
|
return(d) |
|
} |
|
ggplot(data = d, aes_string(x = "PC1", y = "PC2", color = "group", label = "name")) + geom_point(size = 3) + xlab(paste0("PC1: ", round(percentVar[1] * 100), "% variance")) + ylab(paste0("PC2: ", round(percentVar[2] * 100), "% variance")) + coord_fixed() + geom_text_repel(size=3) |
|
|
|
} |
Hi, thanks for your answer, it works perfectly. When I plot, on y axis next to PC3, I have NA variance, any idea why is that? Thank you in advance. EDIT: just a second after I posted, I realized what I was doing wrong :)
glad that it helped and your other problem got resolved..