#加载

library(DMwR2)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(randomForest)
## randomForest 4.7-1.2
## Type rfNews() to see new features/changes/bug fixes.
library(extrafont)
## Registering fonts with R
library(readxl)
#library(pdftools)
library(PerformanceAnalytics)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:randomForest':
## 
##     margin
#library(myplot)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.4     ✔ tibble    3.2.1
## ✔ purrr     1.0.4     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::combine()  masks randomForest::combine()
## ✖ dplyr::filter()   masks stats::filter()
## ✖ dplyr::first()    masks xts::first()
## ✖ dplyr::lag()      masks stats::lag()
## ✖ dplyr::last()     masks xts::last()
## ✖ ggplot2::margin() masks randomForest::margin()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
#library(rstatix)
library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(ggpmisc)
## Loading required package: ggpp
## Registered S3 methods overwritten by 'ggpp':
##   method                  from   
##   heightDetails.titleGrob ggplot2
##   widthDetails.titleGrob  ggplot2
## 
## Attaching package: 'ggpp'
## 
## The following object is masked from 'package:ggplot2':
## 
##     annotate
library(ggpubr)
## 
## Attaching package: 'ggpubr'
## 
## The following objects are masked from 'package:ggpp':
## 
##     as_npc, as_npcx, as_npcy
library(ggsci)
library(ggstar)
library(RColorBrewer)
library(patchwork)
#library(rdacca.hp)
library(ggridges)
library(ggcorrplot)
library(dbplyr) 
## 
## Attaching package: 'dbplyr'
## 
## The following objects are masked from 'package:dplyr':
## 
##     ident, sql
library(dplyr)
library(reshape2)
## 
## Attaching package: 'reshape2'
## 
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(agricolae) 
## 
## Attaching package: 'agricolae'
## 
## The following objects are masked from 'package:PerformanceAnalytics':
## 
##     kurtosis, skewness
library(MASS) 
## 
## Attaching package: 'MASS'
## 
## The following object is masked from 'package:patchwork':
## 
##     area
## 
## The following object is masked from 'package:dplyr':
## 
##     select
#library(ggradar)
library(dplyr)
library(scales)
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
library(tibble)
#library(DMwR2)
library(stringr)
library(psych)
## 
## Attaching package: 'psych'
## 
## The following objects are masked from 'package:scales':
## 
##     alpha, rescale
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
## 
## The following object is masked from 'package:randomForest':
## 
##     outlier
#library(pheatmap)
library(circlize)
## ========================================
## circlize version 0.4.16
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: https://jokergoo.github.io/circlize_book/book/
## 
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization
##   in R. Bioinformatics 2014.
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(circlize))
## ========================================
library(ggplotify)
#library(meta)
#library(metafor)
#library(gemtc)
#library(gghalves)
library(tidyverse)
library(ggstance)
## 
## Attaching package: 'ggstance'
## 
## The following objects are masked from 'package:ggplot2':
## 
##     geom_errorbarh, GeomErrorbarh
library(forcats)
library(EnvStats)
## 
## Attaching package: 'EnvStats'
## 
## The following object is masked from 'package:MASS':
## 
##     boxcox
## 
## The following objects are masked from 'package:agricolae':
## 
##     kurtosis, skewness
## 
## The following objects are masked from 'package:PerformanceAnalytics':
## 
##     kurtosis, skewness
## 
## The following objects are masked from 'package:stats':
## 
##     predict, predict.lm
## 
## The following object is masked from 'package:base':
## 
##     print.default
#library(piecewiseSEM)
#library(basicTrendline)
#library(AICcmodavg)
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
## 
## Attaching package: 'factoextra'
## 
## The following object is masked from 'package:agricolae':
## 
##     hcut
#library(rgl)#3D
#library(ggalluvial)
library(networkD3)
#library(ggsankey)
#library(gapminder)
library(maps)
## 
## Attaching package: 'maps'
## 
## The following object is masked from 'package:purrr':
## 
##     map
library(figpatch)
library(grid)
#library(ggvenn)
library(ggrepel)
library(ggprism)
library(MetBrewer)
#library(treemapify)
#library(ggplotify)
#library(treemap)
library(data.table)
## 
## Attaching package: 'data.table'
## 
## The following objects are masked from 'package:reshape2':
## 
##     dcast, melt
## 
## The following objects are masked from 'package:lubridate':
## 
##     hour, isoweek, mday, minute, month, quarter, second, wday, week,
##     yday, year
## 
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## 
## The following object is masked from 'package:purrr':
## 
##     transpose
## 
## The following objects are masked from 'package:xts':
## 
##     first, last
## 
## The following objects are masked from 'package:zoo':
## 
##     yearmon, yearqtr
#library(do)
library(reticulate)
#library(linkET)
library(ape)
## 
## Attaching package: 'ape'
## 
## The following object is masked from 'package:circlize':
## 
##     degree
## 
## The following object is masked from 'package:agricolae':
## 
##     consensus
## 
## The following object is masked from 'package:ggpubr':
## 
##     rotate
## 
## The following object is masked from 'package:dplyr':
## 
##     where
#library(Biostrings)
#library(treeio)
library(seqinr)
## 
## Attaching package: 'seqinr'
## 
## The following objects are masked from 'package:ape':
## 
##     as.alignment, consensus
## 
## The following object is masked from 'package:agricolae':
## 
##     consensus
## 
## The following object is masked from 'package:dplyr':
## 
##     count
library(phangorn)
#library(ggmsa)
library(picante)
## Loading required package: vegan
## Loading required package: permute
## 
## Attaching package: 'permute'
## 
## The following object is masked from 'package:seqinr':
## 
##     getType
## 
## 
## Attaching package: 'vegan'
## 
## The following objects are masked from 'package:phangorn':
## 
##     diversity, treedist
## 
## The following object is masked from 'package:EnvStats':
## 
##     calibrate
## 
## The following object is masked from 'package:psych':
## 
##     pca
## 
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## 
## The following object is masked from 'package:seqinr':
## 
##     gls
## 
## The following object is masked from 'package:dplyr':
## 
##     collapse
library(phytools)
## 
## Attaching package: 'phytools'
## 
## The following object is masked from 'package:vegan':
## 
##     scores
## 
## The following object is masked from 'package:psych':
## 
##     rescale
## 
## The following object is masked from 'package:scales':
## 
##     rescale
library(geiger)
library(ggtree)
## ggtree v3.16.0 Learn more at https://yulab-smu.top/contribution-tree-data/
## 
## Please cite:
## 
## Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam.
## ggtree: an R package for visualization and annotation of phylogenetic
## trees with their covariates and other associated data. Methods in
## Ecology and Evolution. 2017, 8(1):28-36. doi:10.1111/2041-210X.12628
## 
## Attaching package: 'ggtree'
## 
## The following object is masked from 'package:nlme':
## 
##     collapse
## 
## The following object is masked from 'package:ape':
## 
##     rotate
## 
## The following object is masked from 'package:ggpubr':
## 
##     rotate
## 
## The following object is masked from 'package:tidyr':
## 
##     expand
## 
## The following object is masked from 'package:randomForest':
## 
##     margin
library(caper)
## Loading required package: mvtnorm
library(phylolm)
#library(DMwR)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
## 
## The following object is masked from 'package:randomForest':
## 
##     combine
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
library(rtemis)
##   .:rtemis 0.99.95 🌊 x86_64-w64-mingw32/x64
## 
## Attaching package: 'rtemis'
## 
## The following object is masked from 'package:picante':
## 
##     psd
## 
## The following object is masked from 'package:permute':
## 
##     permute
## 
## The following objects are masked from 'package:psych':
## 
##     describe, logistic, logit
## 
## The following objects are masked from 'package:dplyr':
## 
##     explain, summarize
library(smatr)
library(ggradar)
#调顺序 
# N1 <- within(N1,Organ <- factor(Organ, levels =c("Leaf","Bud", "Stem", "Root"))) 
# with(N1, levels(Organ)) 
# N1 <-
# within(N1,Period <- factor(Period, levels = c("FB", "FF", "PB", "BO")))
# with(N1, levels(Period))
# 
# S2$Study <- as.factor(S2$Study) 

#反转因子顺序 
# scale_y_discrete(limits = rev(levels(df$nitrogen)))
# 
# library(forcats) 
# ggplot(data = df, aes(x = fct_rev(Group), y = Score,
# fill = fct_rev(Sub))) + geom_col(position="dodge") + coord_flip() +
# scale_fill_viridis_d(breaks = rev, direction = -1)
# 
# #添加星号*
# geom_text(aes(y = r+0.05, label = ifelse(p==T,"","*")),size = 10, color = "red", fontface = "bold")
# #异速关系-SMA比较斜率截距差异
# summary(sma(Nmass~Pmass*Group, log='xy', type = "elevation", data=Glopnet_crop)) #P<0.05则不支持H0假设,呈显著
# #添加样本量 
# n_fun <- function(x){
# return(data.frame(y=mean(x)^100-0.23, label = paste0( "(", length(x),")"))) } 
# stat_summary(fun.data = n_fun, geom =
# "text",size=10,color="grey")
# 
# #文本换行
# annotate("text", x = 5, y = 0,family = "Arial",face = "bold", parse = TRUE, size = 4, label = "atop(paste(Slope^'***'),Elevation^ns)")
# #成倍调整坐标轴区间
# scale_y_continuous(expand = c(0.3, 0)) +
# 
# 
# #图例修改2
# scale_fill_manual(values = c("#d1edcd","#94c2db","#fdbeb8"),
#                     label=c(TeX(r"(\textit{h}${^2}$ (all variants)        )"),
#                             TeX(r"(\textit{h}${^2}$ (leading variants))"),
#                             TeX(r"(\textit{h}${^2}$ (local variants)    )")),
#                     name="")
# 
# #AIC值越小,拟合模型更好 
# library(AICcmodavg) 
# library(plyr) 
# library(stringr) 
# ldply(models, function(mod){ data.frame (AICc = AICc(mod), AIC= AIC(mod), model = deparse(formula(mod))) })
# 
# #选择最优拟合2 
# library("basicTrendline") 
# trendline(mpg_new$x, mpg_new$y, model="line2P", ePos.x = "topleft", summary=TRUE,eDigit=5) 
# trendline(mpg_new$x, mpg_new$y, model="line3P", CI.fill =
# FALSE, CI.color = "black", CI.lty = 2) 
# trendline (mpg_new$x, mpg_new$y,model="log2P", ePos.x= "top", linecolor = "red", CI.color = NA)
# trendline (mpg_new$x, mpg_new$y, model="exp2P", CI.fill = FALSE,CI.color = "black", CI.lty = 2) 
# trendline (mpg_new$x, mpg_new$y,model="exp3P", xname="T", yname = paste(delta^15,"N") , yhat=FALSE,
# Rname=1, Pname=0, ePos.x = "bottom")
# 
# #绘图边界调整 
# theme(plot.margin = unit(c(1,1,1,1), "cm"))
# 
# #森林图 tt
# tt=wd%>% group_by(Species)%>% summarise( count=n(),
# mean=mean(Biomass,na.rm = T), sd=sd(Biomass,na.rm = T))
# 
# meta2 <- metamean(count, mean,sd,data=tt, studlab=(tt$Species))
# forest(meta2) 
# forest.meta(meta2)
# 
# 
# 
# #相关性作图  
# library(ggplot2) 
# library(ggcor) 
# set_scale()
# quickcor(mtcars) + geom_square() 
# quickcor(mtcars, type = "upper") +
# geom_circle2() 
# 
# quickcor(mtcars, cor.test = TRUE) + geom_square(data =get_data(type = "lower", show.diag = FALSE)) + geom_mark(data =get_data(type = "upper", show.diag = FALSE), size = 2.5) +
# geom_abline(slope = -1, intercept = 12)
# 
# #矩阵散点相关性 
# install.packages("gpairs") 
# library(gpairs) 
# data(Leaves)
# 
# gpairs(Leaves[,1:4], upper.pars = list(scatter = "stats"), stat.pars = list(fontsize = 9, signif = 0.05, verbose = F, use.color = T),
# lower.pars = list(scatter = "loess"), diagonal = "default", diag.pars =
# list(fontsize = 9, show.hist = TRUE, hist.color = 'black'), axis.pars =
# list(n.ticks = 5, fontsize = 9))
# 
# chart.Correlation(X, histogram=F, pch=19)
# 
# #三元图 
# ggtern(s4,aes(x=L,y=S,z=R,size=T,color=Species))+ geom_point()
# 
# 
# #雷达图3 
# ggradar(df2[,c(3,5:10)], background.circle.colour="skyblue",
# axis.labels=c('LnRR-C','LnRR-N','LnRR-P','LnRR-C:N','LnRR-C:P','LnRR-N:P'))+
# scale_colour_igv()+ #facet_wrap(cols=vars(df2$Species))+ #
# theme(plot.title=element_text(family="Arial", color
# ='black',face='bold',size = 40),
# axis.text.x=element_text(family="Arial",size=28,angle=0,color
# ='black',hjust =0.5,vjust = 0.5),
# axis.text.y=element_text(family="Arial",color ='black',size=28),
# axis.title.x=element_text(family="Arial",color ='black',face='bold',size
# = 40,vjust = -0.5), axis.title.y=element_text(family="Arial",color
# ='black',face='bold',size = 40), strip.text.x =
# element_text(family="Arial",color ='black',face='bold',size = 35),
# strip.text.y = element_text(family="Arial",color
# ='black',face='bold',size = 35), panel.grid = element_blank(),
# panel.background = element_rect(fill = 'transparent', color =
# 'white'),legend.direction = 'vertical' , legend.position = "right",
# legend.text = element_text(family="Arial",size = 20))