R PCA双批次数据子集

R PCA双批次数据子集,r,plot,pca,vegan,R,Plot,Pca,Vegan,我试图为数据子集生成pca双图。 在相同的主成分环境中,我只想根据湿度水平绘制子集 # Packages library(vegan) # Sample data data(dune, dune.env) dd <- cbind(dune.env, dune) # Runnig PCA pca1 <- rda(dd[, -c(1:5)], scale=T) # Plot plot(pca1, scaling=3) # Now, instead the plot above,

我试图为数据子集生成pca双图。 在相同的主成分环境中,我只想根据湿度水平绘制子集

# Packages
library(vegan)

# Sample data
data(dune, dune.env)
dd <- cbind(dune.env, dune)

# Runnig PCA
pca1 <- rda(dd[, -c(1:5)], scale=T)

# Plot
plot(pca1, scaling=3)

# Now, instead the plot above, I'd like to get 5 different plots (one per dd$Moisture level) where I see the principal components scores but only for the subsets based on:
levels(dd$Moisture)
#包
图书馆(素食主义者)
#样本数据
数据(沙丘,沙丘环境)
dd
#包
图书馆(“素食者”)
#准备数据
数据(沙丘,沙丘环境)

dd使用veganggvegan软件包中的
ordixyplot()
,有一些更简单的方法来完成这些分面图(目前,简单的事情有时需要手工完成)

例子 基本图形 对于基础图形,有一个更简单的版本可以在单个绘图上为点上色。一个版本是

scrs <- scores(pca1, display = "sites")
cols <- c("red","green","blue","orange","purple")
plot(scrs[,1], scrs[,2], pch = 19, 
     col = cols[as.numeric(as.character(dune.env$Moisture))])
legend("topright", legend = 1:5, title = "Moisture", pch = 19, 
       col = cols, bty = "n")

scrs这似乎是相关的:感谢您的评论,用户1981275。它引导我回答我自己的问题(我希望它是正确的!:)。@Gavin Simpson。非常感谢您的输入,非常有用!还有你博客上的帖子,我今天已经在用它了,太棒了!尽管颜色很有帮助,我还是希望,比如说,每种颜色都在不同的情节中,但都在相同的PC环境中。由于我的R版本,我无法安装ggvegan。
# Packages
library("vegan")
library("ggvegan")

# Sample data
data(dune, dune.env)

# PCA
pca1 <- rda(dune, scale = TRUE)
## use fortify method to extract scores in ggplot-friendly format
scrs <- fortify(pca1, scaling = 3)
## take only site scores for this
scrs <- with(scrs, scrs[Score == "sites", ])
## add on Moisture variable
scrs <- cbind(scrs, Moisture = dune.env$Moisture)

## for all points on one plot, Moisture coded by colour
plt <- ggplot(scrs, aes(x = Dim1, y = Dim2, colour = Moisture)) + 
         geom_point() + coord_fixed()
plt

## to facet that plot into multiple panels
plt + facet_wrap( ~ Moisture)
## Single plot
ordixyplot(pca1, data = dune.env, formula = PC1 ~ PC2, group = Moisture)

## Facet plot
ordixyplot(pca1, data = dune.env, formula = PC1 ~ PC2 | Moisture)
scrs <- scores(pca1, display = "sites")
cols <- c("red","green","blue","orange","purple")
plot(scrs[,1], scrs[,2], pch = 19, 
     col = cols[as.numeric(as.character(dune.env$Moisture))])
legend("topright", legend = 1:5, title = "Moisture", pch = 19, 
       col = cols, bty = "n")