| Title: | Functions collected/wrote by Daijiang Li |
|---|---|
| Description: | To keep all my functions in one place, and to use them more easily. |
| Authors: | Daijiang Li [aut, cre] |
| Maintainer: | Daijiang Li <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.3 |
| Built: | 2026-05-13 08:37:20 UTC |
| Source: | https://github.com/daijiang/lirrr |
Make the first letter to be upper case.
cap_first_letter(x)cap_first_letter(x)
x |
A vector of species names. |
A vector.
A data frame with site names as row names, species names as column names, cells are the abundance of each species at each site
comm_acomm_a
a data frame with 15 sites and 15 species
A data frame with site names as row names, species names as column names, cells are the abundance of each species at each site
comm_bcomm_b
a data frame with 15 sites and 9 species
Convert a distance class object or a matrix to a data frame.
dist_to_df(x)dist_to_df(x)
x |
An object of class 'dist' or a matrix. |
A data frame with three columns: Var1, Var2, distance.
Calculate the number of polytomies a phylogeny has.
get_n_polytomy(tree)get_n_polytomy(tree)
tree |
A phylogeny with 'phylo' as class. |
A data frame with number of polytomies as columns.
library(lirrr) get_n_polytomy(tree)library(lirrr) get_n_polytomy(tree)
A function to calculate a bunch of phylo diversity: Faith's PD, MPD, VPD (variance of pairwise distance), MNTD, PSV/PSE. Results of MPD/MNTD from PhyloMeasures are equal with those from Phylocom/Picante with abundance.weight = FALSE Results of PD from PhyloMeasures are unrooted; PD from Phylocom are rooted.
get_pd_alpha( samp_wide, tree, samp_long, null.model.pd.root = FALSE, null.model.phylomeasures = TRUE, null.model.phylocom = FALSE, null.type.phylomeasures = "uniform", null.type.phylocom = 0, null.type.picante = "taxa.labels", n.item = 999, abund.weight = FALSE, verbose = TRUE, vpd = FALSE, include.root = FALSE, ... )get_pd_alpha( samp_wide, tree, samp_long, null.model.pd.root = FALSE, null.model.phylomeasures = TRUE, null.model.phylocom = FALSE, null.type.phylomeasures = "uniform", null.type.phylocom = 0, null.type.picante = "taxa.labels", n.item = 999, abund.weight = FALSE, verbose = TRUE, vpd = FALSE, include.root = FALSE, ... )
samp_wide |
Wide version of samp_long, row.names are sites, colnames are species. |
tree |
A phylogeny with class of 'phylo'. |
samp_long |
A 3-column data frame, site, freq, sp. |
null.model.pd.root |
Whether to run null models for rooted PD? |
null.model.phylomeasures |
Whether to run null models using PhyloMeasures package? |
null.model.phylocom |
Whether to run null models using phylocomr package? |
null.type.phylomeasures |
If null.model is TRUE, which null model to use for PhyloMeasure? |
null.type.phylocom |
If null.model is TRUE, which null model to use for Phylocom? - 0: phylogeny shuffle. - 1: maintain site richness and draw from species actually observed in sites. - 2: maintain site richness and draw from the phylogeny - 3: independent swap. See ?phylocomr::ph_comstruct for details |
null.type.picante |
If null.model is TRUE, which null model to use for Picante? Not used yet. |
n.item |
The number of randomization. |
abund.weight |
Should abundance information used when calculating pd with Phylocom/Picante? Default is FALSE. |
verbose |
Do you want to see relevant information? |
vpd |
To calculate vpd (varance of pairwise distance) or not? |
include.root |
Include the root of the tree? |
... |
Additional arguments. |
A data frame.
Highlight all branches linking a subset of species in a phylogeny
highlight_subset_sp_in_phylogeny(phy, subset_sp, highlight_color = "red")highlight_subset_sp_in_phylogeny(phy, subset_sp, highlight_color = "red")
phy |
The large phylogeny to plot. |
subset_sp |
A vector of species names to be highlighted on the plot of phylogeny. |
highlight_color |
The color to highlight the branches, the default is red. |
A 'ggplot2' object.
Conduct logit-transformation for proportion data.
logit_tran(x, add_num = 0.01)logit_tran(x, add_num = 0.01)
x |
A vector of proportions (either in form of 0.20 or 20). |
add_num |
The number to add for 0s, the dafult is 0.01. |
A vector has been logit transformed.
Make grid cells across the lands of world
make_world_grids(cell_size = 1e+05, exclude_Antarctica = TRUE, ...)make_world_grids(cell_size = 1e+05, exclude_Antarctica = TRUE, ...)
cell_size |
The size of the cell, default is 100 km by 100 km. |
exclude_Antarctica |
Exclude Antarctica in the land grid cells? Default is TRUE. |
... |
Additional arguments to be passed into 'sf::st_make_grid()'. |
A list of two sf objectives: 'grid_sf' is the grid cells of the world; 'grid_land' only contains the grid cells that intersected with terrestrial lands.
Calculate MPD and VPD. The distance matrix can be from functional traits or phylogeny. This function is the same as 'picante::mpd' when 'abundance.weighted = FALSE'; it is, however, different when 'abundance.weighted = TRUE' because this function does not include the diagonal values while 'picante::mpd' does, which is actually equal to Rao's Q instead of MPD (de Bello et al, 2016, Oecologia).
mvpd(samp, dis, abundance.weighted = FALSE)mvpd(samp, dis, abundance.weighted = FALSE)
samp |
A site by species data frame, site names as row names. |
dis |
A distance matrix. |
abundance.weighted |
Whether to weight by species abundance? Default is 'FALSE'. |
A data frame with three columns: site, mpd, and vpd.
panel.cor to plot absolute value of correlationspanel.cor to plot absolute value of correlations
panel.cor(x, y, digits = 2, prefix = "", cex.cor, color.threshold = 0.5, ...)panel.cor(x, y, digits = 2, prefix = "", cex.cor, color.threshold = 0.5, ...)
panel.hist to plot absolute value of correlationspanel.hist to plot absolute value of correlations
panel.hist(x, ...)panel.hist(x, ...)
Calculate faith's pd, this is a wrapper of 'phylocomr::ph_pd' and 'picante::pd'.
pd2(comm, tree, include.root = TRUE, comm_long)pd2(comm, tree, include.root = TRUE, comm_long)
comm |
A site by species data frame, site names as row names, only works for presence/absence data. |
tree |
A phylogeny of class "phylo". |
include.root |
Whether include root in picante::pd; phylocomr::ph_pd always include root. |
comm_long |
Long format of comm, 3-columns: site, freq, sp; optional. |
A data frame of PD for each site.
Calculate Phylogenetic beta diversity and its partition, adapted from betapart::phylo.belt.xx(). Since the pairwise and multisie version share the same core computation process, it makes more sense to return both. Then we can choose which one to use.
phylo_betapart(comm = dat_1, tree, ...)phylo_betapart(comm = dat_1, tree, ...)
comm |
A site by species data frame, site names as row names. |
tree |
A phylogeny of class "phylo". |
A list of four: pairwise beta of jaccard and sorense; and multisite beta of jaccard and sorensen. Pairwise beta has three distance matrix. For jaccard, phylo.beta.jtu is the turnover-fraction of Jaccard, phylo.beta.jne is the nestedness-fraction. For sorensen, phylo.beta.sim is the turnover part measured as Simpson derived pairwise dissimilarity, phylo.beta.sne is the nestedness-fraction. Similarly for the multisite version.
Perform randomization test between two vectors.
rand_test(x, y, n = 1000)rand_test(x, y, n = 1000)
x |
A numeric vector. |
y |
A numeric vector. |
n |
The number of randomization, default is 1000. |
a data frame with mean, rank, p.value, etc.
Remove site that has no obsrevations of any species.
rm_site_noobs(df)rm_site_noobs(df)
df |
A data frame in wide form, i.e. site by species data frame, with site names as row name. |
A data frame.
Remove species that not observed in any site.
rm_sp_noobs(df)rm_sp_noobs(df)
df |
A data frame in wide form, i.e. site by species data frame, with site names as row name. |
A data frame.
This function will try to do batch match based on rotl::tnrs_match_names(), which only allows <= 250 species each time.
tnrs_match_names_2(taxa, n_per_req = 20, ...)tnrs_match_names_2(taxa, n_per_req = 20, ...)
taxa |
A vector of species names to match. |
n_per_req |
Number of species to match per requery, must be <= 250. |
A data frame.
A phylogeny with more species than the community data
A 'sf' object, with medium spatial resolution downloaded from rnaturalearth.
tree world_molltree world_moll
Newick format
sf object
calculate unifrac of pairwise site. This is based on picante::unifrac, but with phylocomr::ph_pd to calculate pd, which can improve speed dramatically.
unifrac2(comm, tree, comm_long)unifrac2(comm, tree, comm_long)
comm |
A site by sp data frame, row names are site names. |
tree |
A phylogeny with 'phylo' class. |
comm_long |
A long format of comm, can be missing. |
A site by site distance object.
Convert a column of a data frame to be its row name.
var_to_rownames(df, var = "site")var_to_rownames(df, var = "site")
df |
A data frame. |
var |
The column name (as character) you want to put as row name. |
A data frame.