Hy, after seeing that prcomp plotting can be highly time-consuming, based on the work of Etienne Low-Decarie posted by jlhoward, and adding vector plotting from envfit{vegan} objects (Thanks to Gavin Simpson). I designed a function to create ggplots.
## -> Function for plotting Clustered PCA objects.
### Plotting scores with cluster ellipses and environmental factors
## After: https://stackoverflow.com/questions/20260434/test-significance-of-clusters-on-a-pca-plot
# https://stackoverflow.com/questions/22915337/if-else-condition-in-ggplot-to-add-an-extra-layer
# https://stackoverflow.com/questions/17468082/shiny-app-ggplot-cant-find-data
# https://stackoverflow.com/questions/15624656/labeling-points-in-geom-point-graph-in-ggplot2
# https://stackoverflow.com/questions/14711470/plotting-envfit-vectors-vegan-package-in-ggplot2
# http://docs.ggplot2.org/0.9.2.1/ggsave.html
plot.cluster <- function(scores,hclust,k,alpha=0.1,comp="A",lab=TRUE,envfit=NULL,
save=FALSE,folder="",img.size=c(20,15,"cm")) {
## scores = prcomp-like object
## hclust = hclust{stats} object or a grouping factor with rownames
## k = number of clusters
## alpha = minimum significance needed to plot ellipse and/or environmental factors
## comp = which components are plotted ("A": x=PC1, y=PC2| "B": x=PC2, y=PC3 | "C": x=PC1, y=PC3)
## lab = logical, add label -rownames(scores)- layer
## envfit = envfit{vegan} object
## save = logical, save plot as jpeg
## folder = path inside working directory where plot will be saved
## img.size = c(width,height,units); dimensions of jpeg file
require(ggplot2)
require(vegan)
if ((class(envfit)=="envfit")==TRUE) {
env <- data.frame(scores(envfit,display="vectors"))
env$p <- envfit$vectors$pvals
env <- env[which((env$p<=alpha)==TRUE),]
env <<- env
}
if ((class(hclust)=="hclust")==TRUE) {
cut <- cutree(hclust,k=k)
ggdata <- data.frame(scores, Cluster=cut)
rownames(ggdata) <- hclust$labels
}
else {
cut <- hclust
ggdata <- data.frame(scores, Cluster=cut)
rownames(ggdata) <- rownames(hclust)
}
ggdata <<- ggdata
p <- ggplot(ggdata) +
stat_ellipse(if(comp=="A"){aes(x=PC1, y=PC2,fill=factor(Cluster))}
else if(comp=="B"){aes(x=PC2, y=PC3,fill=factor(Cluster))}
else if(comp=="C"){aes(x=PC1, y=PC3,fill=factor(Cluster))},
geom="polygon", level=0.95, alpha=alpha) +
geom_point(if(comp=="A"){aes(x=PC1, y=PC2,color=factor(Cluster))}
else if(comp=="B"){aes(x=PC2, y=PC3,color=factor(Cluster))}
else if(comp=="C"){aes(x=PC1, y=PC3,color=factor(Cluster))},
size=5, shape=20)
if (lab==TRUE) {
p <- p + geom_text(if(comp=="A"){mapping=aes(x=PC1, y=PC2,color=factor(Cluster),label=rownames(ggdata))}
else if(comp=="B"){mapping=aes(x=PC2, y=PC3,color=factor(Cluster),label=rownames(ggdata))}
else if(comp=="C"){mapping=aes(x=PC1, y=PC3,color=factor(Cluster),label=rownames(ggdata))},
hjust=0, vjust=0)
}
if ((class(envfit)=="envfit")==TRUE) {
p <- p + geom_segment(data=env,aes(x=0,xend=env[[1]],y=0,yend=env[[2]]),
colour="grey",arrow=arrow(angle=15,length=unit(0.5,units="cm"),
type="closed"),label=TRUE) +
geom_text(data=env,aes(x=env[[1]],y=env[[2]]),label=rownames(env))
}
p <- p + guides(color=guide_legend("Cluster"),fill=guide_legend("Cluster")) +
labs(title=paste("Clustered PCA",paste(hclust$call[1],hclust$call[2],hclust$call[3],sep=" | "),
hclust$dist.method,sep="\n"))
if (save==TRUE & is.character(folder)==TRUE) {
mainDir <- getwd ( )
subDir <- folder
if(file.exists(subDir)==FALSE) {
dir.create(file.path(mainDir,subDir),recursive=TRUE)
}
ggsave(filename=paste(file.path(mainDir,subDir),"/PCA_Cluster_",hclust$call[2],"_",comp,".jpeg",sep=""),
plot=p,dpi=600,width=as.numeric(img.size[1]),height=as.numeric(img.size[2]),units=img.size[3])
}
p
}
And as an example, using data(varespec) and data(varechem), note that varespec is transposed to show distance between species:
data(varespec);data(varechem)
require(vegan)
vare.euc <- vegdist(t(varespec),"euc")
vare.ord <- rda(varespec)
vare.env <- envfit(vare.ord,env=varechem,perm=1000)
vare.ward <- hclust(vare.euc,method="ward.D")
plot.cluster(scores=vare.ord$CA$v[,1:3],alpha=0.5,hclust=vare.ward, k=5,envfit=vare.env,save=TRUE)