Biplot of PCA scores with correlation arrows and optional group legend.
PCA_plot <- function(PCoAPlot,PCoACorPlot,multAx1,multAx,title,legend){
Plan12=ggplot(data = data.frame(PCoAPlot$vectors)) +
geom_point(aes(x = Axis.1, y = Axis.2),col="grey89") +
theme_classic()+
ggtitle(title)+
xlab(paste0("PCoA 1 (",round(PCoAPlot$values[1,2]*100,2),"%)"))+
ylab(paste0("PCoA 2 (",round(PCoAPlot$values[2,2]*100,2),"%)"))+
#coord_fixed() + ## need aspect ratio of 1!
geom_segment(data = data.frame(PCoACorPlot),
aes(x = 0, xend = Axis.1*multAx, y = 0, yend = Axis.2*multAx),
arrow = arrow(length = unit(0.25, "cm")), colour = PCoACorPlot$color) +
geom_text(data = data.frame(PCoACorPlot),
aes(x = Axis.1*multAx1, y = Axis.2*multAx1,
label = names), colour = PCoACorPlot$color,
size = 5) +
theme(
plot.title = element_text(
size = 18, # Increase size (adjust as needed)
face = "bold" # Make it bold
)
)
if(!is.null(legend)){
Plan12 = Plan12 + annotate(geom = "text",label = legend, x = Inf, y = -Inf, hjust = 1, vjust = -0.5) # Make text bold)
}
Plan12
}