R在ggplot中可视化cca图,

Pie*_*rez 0 visualization r ggplot2

我在ggplot中可视化cca图有问题.我有一个4种(PF,IF,MM,IM)的数据集和4个站点8个月内采集的3个环境变量,我想知道哪个物种和哪个月/站?哪个环境变量是重要的,我想在ggplot中的一个漂亮的cca图中可视化.我一直在寻找一些R代码,但想象这个似乎很新.

这是我的数据:

在此输入图像描述

我想要一个这样的图表:

在此输入图像描述

Rei*_*son 6

使用ggvegan包这相对容易(^).

首先,您需要安装devtools,然后使用它从github 安装ggvegan包:

install.packages("devtools")
devtools::install_github("gavinsimpson/ggvegan")
Run Code Online (Sandbox Code Playgroud)

接下来,做一个CCA,我在这里使用的例子 ?cca

library("vegan")
library("ggvegan")
data(varespec, varechem)
vare.cca <- cca(varespec, varechem)
Run Code Online (Sandbox Code Playgroud)

然后,要生成CCA triplot的ggplot版本,请使用:

autoplot(vare.cca)
Run Code Online (Sandbox Code Playgroud)

这给出了:

在此输入图像描述

如果您只想将数据放回ggplot-required表单中,请使用以下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
Run Code Online (Sandbox Code Playgroud)

返回的对象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"
Run Code Online (Sandbox Code Playgroud)

^:取决于您想要的定制量...