library(tidyverse)
theme_set(theme_test())
library(vegan)
pca.data <- iris %>% mutate(Sepal.Length = scale(Sepal.Length),
Sepal.Width = scale(Sepal.Width),
Petal.Length = scale(Petal.Length),
Petal.Width = scale(Petal.Width))
pca <- rda(pca.data[,1:4])
pca.summary <- summary(pca)
pca.result <- as.data.frame(pca.summary$sites) %>% select(-PC3,-PC4) %>% mutate(Species = iris$Species)
pca.arrow <- as.data.frame(pca.summary$species) %>% select(-PC3,-PC4)
library(tidyverse)
pca.result%>%ggplot()+
geom_point(aes(x = PC1,y = PC2,fill = Species),shape = 21,size = 4)+
stat_ellipse(aes(x = PC1,y = PC2,color = Species,fill = Species),
geom ="polygon",level = 0.95,size = 0.5,alpha = 0.2)+
geom_segment(data = pca.arrow,aes(x = 0,xend = PC1,y = 0,yend = PC2),
arrow = arrow(length = unit(0.35,"cm")))
ggplot2作图之PCA图
于 2022-09-24 22:10:08 首次发布