如何在 Vegan 中自定义排序图
How do I customize ordination plot in Vegan
我有一个 Vegan 的 CCA,我正在尝试绘制它,但只得到一个非常基本的图,我想知道如何自定义图 - 更改物种单词和箭头的颜色,放入点,更改情节上文字的大小...这是我用来制作 CCA 并绘制它的代码(在素食主义者中):
spe.cca <- cca(spdata~.,env)
plot(spe.cca, choices=c(1,2), display=c('sp','bp'), scaling=2)
spdata 是物种丰度信息,env 是环境数据矩阵。我只想在地块上显示环境变量和物种(不是samples/sites)。
我认为 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," %)"))
我有一个 Vegan 的 CCA,我正在尝试绘制它,但只得到一个非常基本的图,我想知道如何自定义图 - 更改物种单词和箭头的颜色,放入点,更改情节上文字的大小...这是我用来制作 CCA 并绘制它的代码(在素食主义者中):
spe.cca <- cca(spdata~.,env)
plot(spe.cca, choices=c(1,2), display=c('sp','bp'), scaling=2)
spdata 是物种丰度信息,env 是环境数据矩阵。我只想在地块上显示环境变量和物种(不是samples/sites)。
我认为 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," %)"))