R可视化ggplot中的cca图, [英] R visualize cca plot in ggplot,
问题描述
我在ggplot中看到一个cca图的问题。
我有一个包含4个物种(PF,IF,MM,IM)和3个环境变量的数据集,这些变量在8个月期间沿着4个站点获得,
我想查看哪些物种以及哪些月份/站点?哪些环境变量是重要的,
我想在ggplot的一个很好的cca图中看到它。我一直在寻找一些R代码,但它看起来很新,以可视化
这个。
这是我的数据:
我想有这样的图表:
这相对容易( ^ 首先,您需要安装 devtools ,然后使用它来安装 ggvegan 包。
来自github的软件包: install.packages(devtools)
devtools :: install_github(gavinsimpson / ggvegan)
接下来,做一个CCA,在这里我使用<$ c $图书馆(素食)
图书馆(ggvegan )
data(varespec,varechem)
vare.cca< - cca(vare spec,varechem)
然后,要生成一个ggplot版本的CCA triplot,请使用:
autoplot(vare.cca)
这给出:
如果你只是想要数据以ggplot所需的形式返回,使用 fortify()
方法:
fdat < - fortify(vare.cca)
头(fdat)
>头(fdat)
Dim1 Dim2得分标签
1 0.07534683 -0.93580864种Callvulg
2 -0.18133963 0.07610159种Empenigr
3 -1.05354930 -0.06026078种Rhodtome
4 -1.27742838 0.30758571种类Vaccmyrt
5 -0.15256316 0.12053851种类Vaccviti
6 0.24295573 0.26432438物种Pinusylv
由 autoplot()
返回的对象是一个ggplot对象,因此您可以使用该系统添加到图中:
plt < - autoplot(vare.cca)
class(plt)
str(plt,max = 2)
> class(plt)
[1]ggggplot
> str(plt,max = 2)
9
$ data:list()
..- attr(*,class)= chr豁免的列表
$ layers:3
列表$:类'proto','environment'< environment:0xa1847d8>
.. $:类'proto','environment'< environment:0x98ef158>
.. $:类'proto','environment'< environment:0xa0d1cf8>
$ scales:引用类'Scales'[包'ggplot2'],带有1个字段
..和21个方法,其中9个可能相关:
.. add,clone,find, get_scales,has_scale,initialize,input,n,
.. non_position_scales
$ mapping:list()
$ theme:list()
$ coordinates:2
.. $ limits:2
... $ ratio:num 1
..- attr(*,class)= chr [1:3]fixedcartesiancoord
$ facet:1
列表$ shrink:logi TRUE
..- attr(*,class)= chr [1:2]nullfacet
$ plot_env:< environment:R_GlobalEnv>
$ labels:7
列表$ y:chrCCA2
.. $ x:chrCCA1
.. $ shape:chrScore
.. $ color:chrScore
.. $ xend:chrDim1
.. $ yend:chrDim2
.. $ label:chrLabel
- attr(*,class)= chr [1:2]ggggplot
^:取决于您需要多少定制...
I've a problem with visualising a cca plot in ggplot. I've a dataset with 4 species(PF,IF,MM,IM) and 3 environmental variables taken during 8 months along 4 stations, I want to see for which species and which months/stations? which environmental variable(s) is/are important and I want to visualize this in a nice cca plot in ggplot. I've been searching for some R code but it seems quite new to visualize this..
This is my data:
I would like to have a graph like this:
This is relatively easy (^) with the ggvegan package.
First you need to install devtools and then use that to install the ggvegan package from github:
install.packages("devtools")
devtools::install_github("gavinsimpson/ggvegan")
Next, do a CCA, here I'm using the example from ?cca
library("vegan")
library("ggvegan")
data(varespec, varechem)
vare.cca <- cca(varespec, varechem)
Then, to generate a ggplot version of the CCA triplot, use:
autoplot(vare.cca)
This gives:
If you just want the data back in a ggplot-required form, use the fortify()
method:
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
The object returned by autoplot()
is a ggplot object so you can add to the plot using that system:
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"
^: Depending on how much customisation you want...
这篇关于R可视化ggplot中的cca图,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!