[This article was first published on One Tip Per Day
, and kindly contributed to R-bloggers
]. (You can report issue about the content on this page here
Want to share your content on R-bloggers? click here
if you have a blog, or here
if you don't.
When I plotted the PCA results (e.g. scatter plot for PC1 and PC2) and was about to annotate the dataset with different covariates (e.g. gender, diagnosis, and ethic group), I noticed that it’s not straightforward to annotate >2 covariates at the same time using ggplot.
Here is what works for me in ggplot:
pcaData <- plotPCA(vsd, intgroup = c( "Diagnosis", "Ethnicity", "Sex"), returnData = TRUE) # vsd and plotPCA are part of DESeq2 package, nothing with my example below.
percentVar <- round(100 * attr(pcaData, "percentVar"))
ggplot(pcaData, aes(x = PC1, y = PC2, color = factor(Diagnosis), shape = factor(Ethnicity))) +
geom_point(size =3, aes(fill=factor(Diagnosis), alpha=as.character(Sex))) +
geom_point(size =3) +
scale_alpha_manual(values=c(“F”=0, “M”=1)) +
xlab(paste0(“PC1: “, percentVar, “% variance”)) +
ylab(paste0(“PC2: “, percentVar, “% variance”)) +
ggtitle(“PCA of all genes, no covariate adjusted”)
I also found that you can use the male and female symbol (♂ ♀) as shapes in your plot. Here is how:
df <- data.frame(x = runif(10), y = runif(10), sex = sample(c("m","f"), 10, rep = T))
qplot(x, y, data = df, shape = sex, size = I(5)) +
scale_shape_manual(values = c(“m” = “\u2642”, f = “\u2640”))
I’ve not figured out a way to combine the two ideas above.