<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/pca-plot-with-fill-color-and-shape-all-together/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://onetipperday.sterding.com/2018/09/pca-plot-with-fill-color-and-shape-all.html"> One Tip Per Day</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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. </p>
<p>Here is what works for me in ggplot:</p>
<p><span>pcaData &lt;-plotPCA(vsd, intgroup = c( “Diagnosis”, “Ethnicity”, “Sex”), returnData = TRUE) # vsd and plotPCA are part of DESeq2 package, nothing with my example below. </span></p>
<div><span>percentVar &lt;-round(100 * attr(pcaData, “percentVar”)) </span></div>
<div><span>ggplot(pcaData, aes(x = PC1, y = PC2, color = factor(Diagnosis), shape = factor(Ethnicity))) + </span></div>
<div><span>geom_point(size =3, aes(fill=factor(Diagnosis), alpha=as.character(Sex))) + </span></div>
<div><span>geom_point(size =3) + </span></div>
<div><span>scale_shape_manual(values=c(21,22)) + </span></div>
<div><span>scale_alpha_manual(values=c(“F”=0, “M”=1)) + </span></div>
<div><span>xlab(paste0(“PC1: “, percentVar[1], “% variance”)) + </span></div>
<div><span>ylab(paste0(“PC2: “, percentVar[2], “% variance”)) + </span></div>
<div><span>ggtitle(“PCA of all genes, no covariate adjusted”) </span>
<div>I also found that you can use the male and female symbol (<img alt="♂" src="https://s.w.org/images/core/emoji/2.4/72x72/2642.png" class="wp-smiley"> <img alt="♀" src="https://s.w.org/images/core/emoji/2.4/72x72/2640.png" class="wp-smiley">) as shapes in your plot. Here is how:
<p><span>df &lt;-data.frame(x = runif(10), y = runif(10), sex = sample(c(“m”,”f”), 10, rep = T)) </span></div>
<div><span>qplot(x, y, data = df, shape = sex, size = I(5)) + </span></div>
<div><span>scale_shape_manual(values = c(“m” = “\u2642”, f = “\u2640”))</span>
<div>(Reference: https://github.com/kmiddleton/rexamples/blob/master/ggplot2%20male-female%20symbols.R)
<p>I’ve not figured out a way to combine the two ideas above. </p>
</div>
</div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://onetipperday.sterding.com/2018/09/pca-plot-with-fill-color-and-shape-all.html"> One Tip Per Day</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/LpMLZE-i-gs" height="1"></a></div>