2

我还是 R 的新手,正在尝试学习如何使用库 vegan,我可以使用普通的绘图功能轻松地在 R 中绘图。当我想在 ggplot 中绘制数据时,就会出现问题。我知道我必须从我创建的列表中提取正确的数据,但是哪些以及如何?我一直在练习的数据集可以在这里下载https://drive.google.com/file/d/0B1PQGov60aoudVR3dVZBX1VKaHc/view?usp=sharing 我用来转换数据的代码是这样的:

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)

这给了我一个列表,我可以使用基本的“plot”和“biplot”函数绘制,但我不知道如何在 ggplot 中绘制 PCA 和 biplot。我还想按组为数据点着色,例如性别。任何帮助都会很棒。

4

3 回答 3

6

您可以为此使用我的ggvegan包。它仍在开发中,但可用于某些类别的对象,包括rdacca

假设您可以简单地执行示例数据和分析:

autoplot(data2.pca, arrows = TRUE)

得到你想要的那种双标图。这产生

在此处输入图像描述

您可以通过以下方式获取站点标签

autoplot(data2.pca, arrows = TRUE, geom = "text", legend = "none")

它还显示了如何在需要时抑制图例(legend.position采用适合ggplot2中相同主题元素的值)。

在此处输入图像描述

除了使用方法的外观之外,您没有大量的控制权autoplot()(还没有!),但是您可以使用ggplot2fortify()所需的方式获取数据,然后使用其他答案中的想法或研究代码细节。ggvegan:::autoplot.rda

您需要从 github 安装ggvegan,因为该软件包尚未在 CRAN 上:

install.packages("devtools")
devtools::install_github("gavinsimpson/ggvegan")

这将为您提供 0.0-6(或更高版本)版本,其中包括一些小的调整,以产生比以前版本更整洁的图。

于 2015-08-26T16:08:59.573 回答
2

package 中有一个ggbiplot(...)函数ggbiplot,但它只适用于 prcomp、princomp、PCA 或 lda 类的对象。

plot.rda(...)只需在 PC1 - PC2 空间中定位每个案例(人)。biplot.rda(...)将向量添加到原始数据集中每个变量的 PC1 和 PC2 载荷。事实证明,plot.rda(...)biplot.rda(...)使用通过汇总 rda 对象产生的数据,而不是 rda 对象本身。

smry <- summary(data2.pca)
df1  <- data.frame(smry$sites[,1:2])       # PC1 and PC2
df2  <- data.frame(smry$species[,1:2])     # loadings for PC1 and 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

如果你比较这些结果plot(data2.pca)biplot(data2.pca)我想你会发现它们是一样的。信不信由你,到目前为止,最难的部分是让文本正确对齐箭头。

于 2015-08-25T04:58:12.687 回答
2

根据@jlhoward,您可以ggbiplot从具有相同名称的包中使用。那么您唯一需要做的就是将您的rda结果转换prcomp为已知的结果ggbiplot。这是一个执行此操作的函数:

#' Cast vegan::rda Result to base::prcomp
#'
#' Function casts a result object of unconstrained
#' \code{\link[vegan]{rda}} to a \code{\link{prcomp}} result object.
#'
#' @param x An unconstrained \code{\link[vegan]{rda}} result object.
#'
#' @importFrom vegan scores
#' @export
`as.prcomp.rda` <-
    function(x)
{
    if (!is.null(x$CCA) || !is.null(x$pCCA))
        stop("works only with unconstrained rda")
    structure(
        list(sdev = sqrt(x$CA$eig),
             rotation = x$CA$v,
             center = attr(x$CA$Xbar, "scaled:center"),
             scale = if(!is.null(scl <- attr(x$CA$Xbar, "scaled:scale")))
                         scl
                     else
                         FALSE,
             x = scores(x, display = "sites", scaling = 1,
             choices = seq_len(x$CA$rank),
             const = sqrt(x$tot.chi * (nrow(x$CA$u)-1)))),
        class = "prcomp")
}
于 2015-08-26T10:17:18.900 回答