Skip to content

Instantly share code, notes, and snippets.

@mdozmorov
Created March 17, 2019 01:13
Show Gist options
  • Save mdozmorov/3dda64fada07938159afa4532d8c1a63 to your computer and use it in GitHub Desktop.
Save mdozmorov/3dda64fada07938159afa4532d8c1a63 to your computer and use it in GitHub Desktop.
PCA example
# PCA: Check for batch effects. Select one batch, to color points by its assignment
pca <- mtx %>% varFilter(., var.cutoff = 0.75) %>% scale %>% t %>% prcomp
colorby <- "Group" # covariates[2]
pt <- ggplot(data = data.frame(pca$x, annot),
aes(x = as.numeric(PC1), y = as.numeric(PC2), label = Sample)) +
theme(plot.title = element_text(lineheight = 0.8, face="bold")) +
ggtitle(paste("PCA with batch, coloring by ", colorby)) +
geom_point(aes(color = eval(parse(text = colorby))), size = 3) +
geom_text_repel(colour = "black", size = 3) +
geom_hline(yintercept = 0, colour = "gray65") +
geom_vline(xintercept = 0, colour = "gray65") +
labs(color = colorby) +
scale_x_continuous(name = paste0("PC1, ", round(summary(pca)$importance[2,1] * 100, digits = 2), "% variability" )) +
scale_y_continuous(name = paste0("PC2, ", round(summary(pca)$importance[2,2] * 100, digits = 2), "% variability" )) +
theme(legend.position="none")
plot(pt)
ggsave(filename = "Figures/Figure_PCA_withbatch.pdf", plot = pt, height = 8, width = 11)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment