Question

I've found somewhere here code to create PCA biplot in ggplot2. I've made some small modifications, but I still need one improvement.

The code looks like that:

PCbiplot2 <- function(res.pca, x="Dim.1", y="Dim.2") {
if(!require(ggplot2)) install.packages("ggplot2")
# res.pca being a PCA object
data <- data.frame(obsnames=row.names(res.pca$ind$coord), res.pca$ind$coord)
plot <- ggplot(data, aes_string(x=x, y=y)) + geom_text(size=3,     aes(label=obsnames))
plot <- plot + geom_hline(aes(0), size=.2) + geom_vline(aes(0), size=.2)
datapc <- data.frame(varnames=rownames(res.pca$var$coord), res.pca$var$coord)
mult <- min(
(max(data[,y]) - min(data[,y])/(max(datapc[,y])-min(datapc[,y]))),
(max(data[,x]) - min(data[,x])/(max(datapc[,x])-min(datapc[,x])))
)
datapc <- transform(datapc,
                  v1 = .7 * mult * (get(x)),
                  v2 = .7 * mult * (get(y))
)
plot <- plot + coord_equal() + geom_text(data=datapc, aes(x=v1, y=v2, label=varnames), size = 3, vjust=1.5, color="red")
plot <- plot + geom_segment(data=datapc, aes(x=0, y=0, xend=v1, yend=v2),     arrow=arrow(length=unit(0.2,"cm")), alpha=0.75, color="red")
plot <- plot + theme(panel.background = element_rect(fill='white', colour='black'))
plot
}

Using this code axis names in the graph are "Dim.1" and "Dim.2". I need to show the percentage of dimensions variability. This information is in res.pca$eig[1,2] for Dim.1 and res.pca$eig[2,2] for Dim.2 but I have no idea how to enter this information to the graph.

Was it helpful?

Solution

plot<-plot+xlab("axis name") 

maybe?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top