Shared a previous tweet
The content is to repeat the variance decomposition process of a Nature Ecology&Evolution
journal paper. Although I still don't understand the operation process, I can get the results. In today's tweet, we will use the obtained results to reproduce the right half of Figure 4a in the paper, the stacked column chart on the left, and how to combine the two graphs for a separate tweet introduction (in fact, it was recently. A little 创作
tired, the content of one tweet is divided into two hahahaha)
The first is to run the code of the previous tweet to get the drawing data
datatotal<-read.table("datasetmultifunctionality.txt", header=T, sep="\t")
colnames(datatotal)
#####logtransformation moments
datatotal[,c(12,13,16,17)]<-log(datatotal[,c(12,13,16,17)])
datatotal[,14]<-log(datatotal[,14]-min(datatotal[,14])+1)
datatotal[,15]<-log(datatotal[,15]-min(datatotal[,15])+1)
datatotal[,18]<-log(datatotal[,18]-min(datatotal[,18])+1)
datatotal[,19]<-log(datatotal[,19]-min(datatotal[,19])+1)
#####Zscorring environmental variables
datatotal$ELEVATION<-(datatotal$ELEVATION-mean(datatotal$ELEVATION))/sd(datatotal$ELEVATION)
datatotal$LAT<-(datatotal$LAT-mean(datatotal$LAT))/sd(datatotal$LAT)
datatotal$SINLONG<-(datatotal$SINLONG-mean(datatotal$SINLONG))/sd(datatotal$SINLONG)
datatotal$COSLONG<-(datatotal$COSLONG-mean(datatotal$COSLONG))/sd(datatotal$COSLONG)
datatotal$SLO<-(datatotal$SLO-mean(datatotal$SLO))/sd(datatotal$SLO)
datatotal$ARIDITY<-(datatotal$ARIDITY-mean(datatotal$ARIDITY))/sd(datatotal$ARIDITY)
datatotal$SAND<-(datatotal$SAND-mean(datatotal$SAND))/sd(datatotal$SAND)
datatotal$PH<-(datatotal$PH-mean(datatotal$PH))/sd(datatotal$PH)
datatotal$SR<-(datatotal$SR-mean(datatotal$SR))/sd(datatotal$SR)
#####Zscorring moments
datatotal$CWM_logH<-(datatotal$CWM_logH-mean(datatotal$CWM_logH))/sd(datatotal$CWM_logH)
datatotal$CWV_logH<-(datatotal$CWV_logH-mean(datatotal$CWV_logH))/sd(datatotal$CWV_logH)
datatotal$CWS_logH<-(datatotal$CWS_logH-mean(datatotal$CWS_logH))/sd(datatotal$CWS_logH)
datatotal$CWK_logH<-(datatotal$CWK_logH-mean(datatotal$CWK_logH))/sd(datatotal$CWK_logH)
datatotal$CWM_logSLA<-(datatotal$CWM_logSLA-mean(datatotal$CWM_logSLA))/sd(datatotal$CWM_logSLA)
datatotal$CWV_logSLA<-(datatotal$CWV_logSLA-mean(datatotal$CWV_logSLA))/sd(datatotal$CWV_logSLA)
datatotal$CWS_logSLA<-(datatotal$CWS_logSLA-mean(datatotal$CWS_logSLA))/sd(datatotal$CWS_logSLA)
datatotal$CWK_logSLA<-(datatotal$CWK_logSLA-mean(datatotal$CWK_logSLA))/sd(datatotal$CWK_logSLA)
#####Zscorring ecosystem functions
datatotal$BGL<-(datatotal$BGL-mean(datatotal$BGL))/sd(datatotal$BGL)
datatotal$FOS<-(datatotal$FOS-mean(datatotal$FOS))/sd(datatotal$FOS)
datatotal$AMP<-(datatotal$AMP-mean(datatotal$AMP))/sd(datatotal$AMP)
datatotal$NTR<-(datatotal$NTR-mean(datatotal$NTR))/sd(datatotal$NTR)
datatotal$I.NDVI<-(datatotal$I.NDVI-mean(datatotal$I.NDVI))/sd(datatotal$I.NDVI)
#####Calculating indices of multifunctionality (M5: 5 functions)
colnames(datatotal)
M5<-rowMeans(datatotal[,c(20,21,22,23,24)])
datatotal<-cbind(datatotal,M5)
#####Log-transfromation of multifunctionality
logM5<-log(datatotal$M5-min(datatotal$M5)+1)
datatotal<-cbind(datatotal,logM5)
library(MuMIn)
mod12<-lm(logM5 ~ LAT + SINLONG + COSLONG +
ARIDITY + SLO + SAND + PH + I(PH^2) + ELEVATION+
CWM_logSLA + I(CWM_logSLA^2)+ CWV_logSLA + I(CWV_logSLA^2) + CWS_logSLA + CWK_logSLA + I(CWK_logSLA^2) +
CWM_logH + I(CWM_logH^2)+ CWV_logH + I(CWV_logH^2) + CWS_logH + CWK_logH + I(CWK_logH^2) +
SR
, data=datatotal)
# 这一步要好长时间
dd12<-dredge(mod12, subset = ~ LAT & SINLONG & COSLONG & ARIDITY & SLO & SAND & PH &SR & ELEVATION &
dc(CWM_logSLA,I(CWM_logSLA^2)) & dc(CWV_logSLA,I(CWV_logSLA^2)) & dc(CWK_logSLA,I(CWK_logSLA^2))
& dc(CWM_logH,I(CWM_logH^2)) & dc(CWV_logH,I(CWV_logH^2)) & dc(CWK_logH,I(CWK_logH^2)),
options(na.action = "na.fail"))
subset(dd12,delta<2)
de12<-model.avg(dd12, subset = delta < 2)
summary(de12)
This part of the code and sample data can be obtained by replying to 20210403 in the background of the official account
Next is drawing
The first is to get the drawing data
load("de12.Rdata")
library(tidyverse)
as.data.frame(de12.summary$coefmat.subset) %>%
mutate(var=rownames(.),
group=sample(LETTERS[1:4],22,replace = T)) %>%
rows_delete(tibble(var="(Intercept)")) -> mydf
The grouping files here are constructed by me first.
Add significance asterisks and set factor levels
mydf %>%
arrange(group,Estimate) %>%
mutate(signi=case_when(
`Pr(>|z|)` > 0.05 ~ '',
`Pr(>|z|)` < 0.05 & `Pr(>|z|)` >= 0.01 ~ '*',
`Pr(>|z|)` < 0.01 & `Pr(>|z|)` >= 0.001 ~ '**',
`Pr(>|z|)` < 0.001 ~ '***'
)) %>%
mutate(var=fct_relevel(var,var)) -> mydf1
Finally the drawing code
library(ggplot2)
library(ggh4x)
library(see)
ggplot(mydf1,aes(x=Estimate,y=var))+
geom_point(aes(color=group),
show.legend = F,
size=5)+
xlim(-0.2,0.2)+
labs(y=NULL)+
theme_minimal()+
theme(panel.grid = element_blank(),
axis.text.y = element_blank(),
axis.line.x = element_line(),
axis.ticks.x = element_line())+
guides(x=guide_axis_truncated(trunc_lower = -0.2,
trunc_upper = 0.2))+
geom_linerange(aes(xmin=Estimate-`Std. Error`,
xmax=Estimate+`Std. Error`,
color=group),
show.legend = F)+
geom_text(aes(y=1:21,x=0.1,label=var),hjust=0)+
geom_text(aes(y=1:21,x=0.19,label=signi))+
scale_color_material_d()
The final result is as follows
Of course, there are still some details to be adjusted. We will introduce them together in the next tweet.
The sample data and code for drawing can be obtained by replying to 20210910 in the background of the official account
Finally, I wish the teachers who pay attention to the public account a happy Teacher Day! The research goes well! The article keeps posting!
Welcome everyone to pay attention to my public number
Xiao Ming's data analysis notebook
Xiaoming’s data analysis notebook public account mainly shares: 1. Simple examples of R language and python for data analysis and data visualization; 2. Reading notes on horticultural plants related transcriptomics, genomics, and population genetics literature; 3. Bioinformatics Learn introductory study materials and your own study notes!
This article is shared from the WeChat public account - Xiao Ming's data analysis notebook (gh_0c8895f349d3).
If there is any infringement, please contact [email protected] to delete it.
This article participates in the " OSC Yuanchuang Project ", you are welcome to join and share with us.