Commit 2225676a authored by Holger Brandl's avatar Holger Brandl

more generic pca-plot

parent a24c4068
......@@ -352,6 +352,10 @@ distinct_all = function (x, ...) distinct(x, ..., .keep_all=T)
get_col = function(data, col_index) data[, col_index] ## also could use magrittr::extract here
## convience method to extract a column, defaults to _ as separator and the first column
extract_col = function(x, col_index=1, sep="_", num_cols=10){ str_split_fixed(x, sep, num_cols)[,col_index] }
reload_dplyr <- function(){
unloadNamespace('tidyr')
......
......@@ -115,17 +115,11 @@ ggsave2 <- function(gplot=last_plot(), width=8, height=6, prefix="", saveData=FA
########################################################################################################################
### pca plots (http://largedata.blogspot.de/2011/07/plotting-pca-results-in-ggplot2.html)
makePcaPlot <- function(x = getData(), group = NA, items=rownames(x), title = "") {
require(ggplot2)
require(RColorBrewer)
# data <- x
# data <- t(apply(data, 1, scale))
# rownames(data) <- rownames(x)
# colnames(data) <- colnames(x)
# mydata <- t(data)
mydata <-x
mydata.pca <- prcomp(mydata, retx=TRUE, center=TRUE, scale.=TRUE)
makePcaPlot = function(matrixData, group = NA, items=rownames(matrixData), title = NA) {
load_pack(ggplot2)
load_pack(RColorBrewer)
mydata.pca = prcomp(matrixData, retx=TRUE, center=TRUE, scale.=TRUE)
percent <- round((((mydata.pca$sdev)^2 / sum(mydata.pca$sdev^2))*100)[1:2])
......@@ -133,18 +127,17 @@ makePcaPlot <- function(x = getData(), group = NA, items=rownames(x), title = ""
pc12 <- data.frame(PCA1=scores[,1], PCA2=scores[,2], group=group)
# ggplot(pc12, aes(PCA1, PCA2, colour = group)) + geom_point(size = 6, alpha = 3/4)
ggplot(pc12, aes(PCA1, PCA2, colour = group, label=items)) +
geom_point(size = 6, alpha = 3/4) +
geom_text(size = 6, alpha = 3/4) +
pcaPlot = ggplot(pc12, aes(PCA1, PCA2, color=group, label=items)) +
geom_point(alpha = 3/4) +
geom_text(hjust = 0, alpha = 3/4, nudge_x = 1) +
xlab(paste("PCA1 (", percent[2], "%)", sep = "")) +
ylab(paste("PCA2 (", percent[2], "%)", sep = ""))
qplot(PCA2, PCA1, geom="blank", main = title, xlab = paste("PCA2 (", percent[2], "%)", sep = ""), ylab = paste("PCA1 (", percent[1], "%)", sep = "")) +
geom_point(aes(colour = group), size = 6, alpha = 3/4)
# theme(
# axis.text.x = element_text(size = base_size * 1.3 , lineheight = 0.9, colour = "grey50", hjust = 1, angle = 90),
# axis.text.y = element_text(size = base_size * 1.3, lineheight = 0.9, colour = "grey50", hjust = 1)
# )
if(!is.na(title))
pcaPlot = pcaPlot + ggtitle(title)
pcaPlot
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment