2015-02-23 137 views
0

我在ggplot中可視化cca圖時遇到了問題。 我有一個包含4個物種(PF,IF,MM,IM)和3個環境變量的數據集,它們沿着4個站在8個月期間採集, 我想查看哪些物種以及哪些月份/站點?哪些環境變量是重要的,我想在ggplot的一個很好的cca圖中看到它。我一直在尋找一些R代碼裏面,但它似乎很新的可視化 這個..R可視化ggplot中的cca圖,

這是我的數據:

enter image description here

我想有這樣一個圖:

enter image description here

+3

請不要將您的圖像放到SPAM /彈出窗口顯示網站。當然,不要使用圖像發送給我們的數據片段 - 只需將文本複製/粘貼到Q. – 2015-02-23 20:09:02

回答

4

這與ggvegan PA相對容易(^) ckage。

首先,你需要安裝devtools,然後用它來安裝從GitHub的ggvegan包:

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

接下來,做了CCA,這裏我使用的是從?cca

的例子
library("vegan") 
library("ggvegan") 
data(varespec, varechem) 
vare.cca <- cca(varespec, varechem) 

然後,生成CCA triplot的ggplot版本,使用:

autoplot(vare.cca) 

這給:

enter image description here

如果你只想把數據傳回在ggplot要求的形式,使用fortify()方法:

fdat <- fortify(vare.cca) 
head(fdat) 

> head(fdat) 
     Dim1  Dim2 Score Label 
1 0.07534683 -0.93580864 species Callvulg 
2 -0.18133963 0.07610159 species Empenigr 
3 -1.05354930 -0.06026078 species Rhodtome 
4 -1.27742838 0.30758571 species Vaccmyrt 
5 -0.15256316 0.12053851 species Vaccviti 
6 0.24295573 0.26432438 species Pinusylv 

通過autoplot()返回的對象是一個ggplot對象,因此您可以使用該系統添加到繪圖中:

plt <- autoplot(vare.cca) 
class(plt) 
str(plt, max = 2) 

> class(plt) 
[1] "gg"  "ggplot" 
> str(plt, max = 2) 
List of 9 
$ data  : list() 
    ..- attr(*, "class")= chr "waiver" 
$ layers  :List of 3 
    ..$ :Classes 'proto', 'environment' <environment: 0xa1847d8> 
    ..$ :Classes 'proto', 'environment' <environment: 0x98ef158> 
    ..$ :Classes 'proto', 'environment' <environment: 0xa0d1cf8> 
$ scales  :Reference class 'Scales' [package "ggplot2"] with 1 fields 
    ..and 21 methods, of which 9 are possibly relevant: 
    .. add, clone, find, get_scales, has_scale, initialize, input, n, 
    .. non_position_scales 
$ mapping : list() 
$ theme  : list() 
$ coordinates:List of 2 
    ..$ limits:List of 2 
    ..$ ratio : num 1 
    ..- attr(*, "class")= chr [1:3] "fixed" "cartesian" "coord" 
$ facet  :List of 1 
    ..$ shrink: logi TRUE 
    ..- attr(*, "class")= chr [1:2] "null" "facet" 
$ plot_env :<environment: R_GlobalEnv> 
$ labels  :List of 7 
    ..$ y  : chr "CCA2" 
    ..$ x  : chr "CCA1" 
    ..$ shape : chr "Score" 
    ..$ colour: chr "Score" 
    ..$ xend : chr "Dim1" 
    ..$ yend : chr "Dim2" 
    ..$ label : chr "Label" 
- attr(*, "class")= chr [1:2] "gg" "ggplot" 

^:取決於您需要多少定製...

+0

另外,我假定您可以或想要使用**素食主義者**來執行CCA。否則,你是獨立的;您需要將數據按照與我在'head(fdat)'行顯示的格式相同的方式進行處理,方法是從您用來配合CCA的任何軟件中提取信息。 – 2015-02-23 20:24:52

+0

好的,謝謝,我已經使用autoplot,但我想進一步定製的情節,添加物種標籤,添加羣集(月),改變面板背景顏色.. – 2015-02-24 06:15:17

+0

或你的意思是如果我使用強化我可以使用它像一個正常的ggplot並定製它?但是如何打開標籤呢? – 2015-02-24 06:17:06