2015-08-25 55 views
2

我還是R的新手,試圖學習如何使用庫素食主義者,我可以使用正常的繪圖功能輕鬆繪製R圖。當我想在ggplot中繪製數據時出現問題。我知道我必須從我創建的列表中提取正確的數據,但是哪些和如何?我一直在練習上的數據集可以在這裏https://drive.google.com/file/d/0B1PQGov60aoudVR3dVZBX1VKaHc/view?usp=sharing 下載我一直在使用得到轉化數據的代碼是這樣的:在ggplot中繪製RDA(素食主義者)

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) 

,給了我一個名單,我可以積使用基本「劇情」和「biplot」函數,但是我不知道如何在ggplot中繪製PCA和雙標圖。我還想按組對數據點着色,例如,性別。任何幫助都會很棒。

回答

1

程序包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)我想你會看到他們是相同的。到目前爲止,相信它或者不是最難的部分,就是讓文本與箭頭正確對齊。

+0

謝謝你的回答@jlhoward,雖然雙標圖與我從雙標圖函數中得到的不同 - 在我的雙標圖中某些矢量更長?文字還是很煩人的,我猜這有沒有簡單的解決辦法呢? –

+0

該解決方案的主要問題是軸沒有相等的寬高比。我自己不使用'ggplot2',但它必須有一種設置寬高比的方法。使用'scores()'來訪問縮放的排序結果也更好 - 'summary()'是如此的複雜。 –

+2

也許有助於看看https://github.com/gavinsimpson/ggvegan中的ggvegan包 –

2

根據@jlhoward,您可以使用包名中的ggbiplot。那麼你需要做的唯一事情就是將rda結果投射到ggbiplot已知的結果prcomp。下面是一個函數來做到這一點:

#' 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") 
} 
2

你可以用我ggvegan包這一點。它仍在開發中,但可用於某些類別的對象,包括rdacca

假設的例子中的數據和分析,你可以簡單地做:

autoplot(data2.pca, arrows = TRUE) 

得到你想要的那種雙標圖的。這將產生

enter image description here

您可以通過

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

這也說明了如何在需要抑制的傳說得到網站的標籤(legend.position採取適合於同一主題元素值GGPLOT2)。

enter image description here

你沒有一個巨大的控制其他的事情autoplot()方法外觀量(呢!),但你可以使用fortify()獲取數據GGPLOT2需要的方式,然後使用來自其他答案的想法或針對具體細節研究ggvegan:::autoplot.rda的代碼。

你需要從GitHub安裝ggvegan作爲包尚未在CRAN:

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

這將讓你的版本0.0-6(或更高版本),其中包括一些小的調整,產生整潔地塊比以前的版本。

相關問題