Created
March 24, 2018 03:09
-
-
Save dgrapov/2fd3027d2bd7f15884b2d40a82d50c06 to your computer and use it in GitHub Desktop.
basic principal components analysis and visualization in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Basic PCA example | |
# use www.createdatasol.com for | |
# an advanced user interface | |
#required packages for plotting | |
library(ggplot2) | |
library(ggrepel) | |
#load data | |
data<-read.csv('~/Sampledata.csv', | |
header = TRUE,stringsAsFactors = FALSE) | |
#split numeric and text | |
is_num<-!sapply(data,class) %in% c('character','factor') | |
#calculate principal components | |
pca<-prcomp(data[,is_num]) | |
# get sample scores and | |
# add meta data for visualization | |
plot_data<-cbind(data[,!is_num],pca$x) | |
#plot scores | |
ggplot(plot_data, aes(x=PC1,y=PC2)) + | |
geom_point(aes(color=Label,size=Age),alpha=.75) + | |
geom_text_repel(aes(label=ID),size=1.5) + | |
theme_classic() |
Author
dgrapov
commented
Mar 24, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment