首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Vegan中自定义排序图

如何在Vegan中自定义排序图
EN

Stack Overflow用户
提问于 2019-11-20 22:48:51
回答 1查看 592关注 0票数 0

我有一个CCA在素食,并试图绘制它,但只得到一个非常基本的情节,我想知道我如何才能自定义的情节-改变颜色的物种单词和箭头,在点,改变大小的单词在绘图上…这是我用来制作CCA并绘制它的代码(在Vegan中):

代码语言:javascript
复制
spe.cca <- cca(spdata~.,env)
plot(spe.cca, choices=c(1,2), display=c('sp','bp'), scaling=2)

spdata是物种丰富的信息,env是环境数据的矩阵。我只想在图上显示环境变量和物种(而不是样本/站点)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-07 00:13:47

我认为ggfortify可以帮助你做到这一点。您可以使用物种丰富度和环境变量的CCA1和CCA2得分来绘制所需的文本和箭头。与任何ggplot一样,您可以为绘图中的每个元素设置颜色、大小等。唯一的细节是,您可能需要缩放环境变量位置和箭头,以便获得与plot(spe.cca)生成的非常相似的图。以下是使用varechem和varespec数据集的代码

代码语言:javascript
复制
library(vegan)
library(ggfortify)

data(varespec)
data(varechem)

#CCA
cca_model<-cca(varespec ~ .,data=varechem)
plot(cca_model,choices=c(1,2), display=c('sp','bp'), scaling=2)

#Get CCA scores
df_species  <- data.frame(summary(cca_model)$species[,1:2])# get the species CC1 and CC2 scores
df_environ  <- scores(cca_model, display = 'bp') #get the environment vars CC1 and CC2 scores

cca1_varex<-round(summary(cca_model)$cont$importance[2,1]*100,2) #Get percentage of variance explained by first axis
cca2_varex<-round(summary(cca_model)$cont$importance[2,2]*100,2) #Get percentage of variance explained by second axis

#Set a scaling variable to multiply the CCA values, in order to get a very similar plot to the the one generated by plot(cca_model). You can adjust it according to your data
scaling_factor <- 2

ggplot(df_species, 
       aes(x=CCA1, y=CCA2)) + 
  #Draw lines on x = 0 and y = 0
  geom_hline(yintercept=0, 
             linetype="dashed") +
  geom_vline(xintercept=0, 
             linetype="dashed") +
  coord_fixed()+
  #Add species text
  geom_text(data=df_species, 
            aes(x=CCA1,#Score in CCA1 to add species text
                y=CCA2,#Score in CCA2 to add species text
                label=rownames(df_species),
                hjust=0.5*(1-sign(CCA1)),#Set the text horizontal alignment according to its position in the CCA plot
                vjust=0.5*(1-sign(CCA2))),#Set the text vertical alignment according to its position in the CCA plot
            color = "forestgreen")+
  #Add environmental vars arrows
  geom_segment(data=df_environ, 
               aes(x=0, #Starting coordinate in CCA1 = 0 
                   xend=CCA1*scaling_factor,#Ending coordinate in CCA1  
                   y=0, #Start in CCA2 = 0
                   yend=CCA2*scaling_factor), #Ending coordinate in CCA2 
               color="firebrick1", #set color
               arrow=arrow(length=unit(0.01,"npc"))#Set the size of the lines that form the tip of the arrow
               )+
  #Add environmental vars text
  geom_text(data=df_environ, 
            aes(x=CCA1*scaling_factor, 
                y=CCA2*scaling_factor,
                label=rownames(df_environ),
                hjust=0.5*(1-sign(CCA1)),#Add the text of each environmental var at the end of the arrow
                vjust=0.5*(1-sign(CCA2))),#Add the text of each environmental var at the end of the arrow 
            color="firebrick1")+
  #Set bw theme
  theme_bw()+
  #Set x and y axis titles
  labs(x=paste0("CCA1 (",cca1_varex," %)"),
       y=paste0("CCA2 (",cca2_varex," %)"))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58957101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档