r - Plotting RDA (vegan) in ggplot -
i'm still new r, trying learn how use library vegan, can plot in r normal plot function. problem arises when want plot data in ggplot. know have extract right data list i've created, , how? dataset i've been practicing on can downloaded here https://drive.google.com/file/d/0b1pqgov60aoudvr3dvzbx1vkahc/view?usp=sharing code i've been using data transformed this:
library(vegan) library(dplyr) library(ggplot2) library(grid) data <- read.csv(file = "people.csv", header = t, sep = ",", dec = ".", check.names = f, na.strings=c("na", "-", "?")) data2 <- data[,-1] rownames(data2) <- data[,1] data2 <- scale(data2, center = t, scale = apply(data2, 2, sd)) data2.pca <- rda(data2)
which gives me list can plot using basic "plot" , "biplot" function, @ loss how plot both pca , biplot in ggplot. color data points group, e.g. sex. great.
there ggbiplot(...)
function in package ggbiplot
, works objects of class prcomp, princomp, pca, or lda.
plot.rda(...)
locates each case (person) in pc1 - pc2 space. biplot.rda(...)
adds vectors pc1 , pc2 loadings each variable in original dataset. turns out plot.rda(...)
, biplot.rda(...)
use data produced summarizing rda object, not rda object itself.
smry <- summary(data2.pca) df1 <- data.frame(smry$sites[,1:2]) # pc1 , pc2 df2 <- data.frame(smry$species[,1:2]) # loadings pc1 , pc2 rda.plot <- ggplot(df1, aes(x=pc1, y=pc2)) + geom_text(aes(label=rownames(df1)),size=4) + geom_hline(yintercept=0, linetype="dotted") + geom_vline(xintercept=0, linetype="dotted") + coord_fixed() rda.plot
rda.biplot <- rda.plot + geom_segment(data=df2, aes(x=0, xend=pc1, y=0, yend=pc2), color="red", arrow=arrow(length=unit(0.01,"npc"))) + geom_text(data=df2, aes(x=pc1,y=pc2,label=rownames(df2), hjust=0.5*(1-sign(pc1)),vjust=0.5*(1-sign(pc2))), color="red", size=4) rda.biplot
if compare these results plot(data2.pca)
, biplot(data2.pca)
think you'll see same. believe or not hardest part, far, getting text align wrt arrows.
Comments
Post a Comment