我如何在素食主义者中自定义排序图

问题描述 投票:0回答:1

我有一个纯素食主义者的CCA,正在尝试绘制它,但是只有一个非常基本的图,我想知道如何自定义图-改变物种单词和箭头的颜色,加点,改变单词的大小在情节上...这是我用来制作CCA并将其作图的代码(在纯素食主义者中):

spe.cca <- cca(spdata~.,env)
plot(spe.cca, choices=c(1,2), display=c('sp','bp'), scaling=2)

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

r plot base vegan
1个回答
0
投票

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

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," %)"))
© www.soinside.com 2019 - 2024. All rights reserved.