diff --git a/R/plot.R b/R/plot.R index 71a99af..3209aab 100644 --- a/R/plot.R +++ b/R/plot.R @@ -444,6 +444,10 @@ plot_ancestral_states <- function( #' network is represented, otherwise a weighted network is assumed. #' @param tree The phylogeny of the symbiont clade (e.g. parasites, herbivores). Object of class `phylo`. #' @param host_tree The phylogeny belonging to the hosts. Object of class `phylo`. +#' @param widths Used in a call to `patchwork::wrap_plots()`. The relative widths of each column +#' in the plot grid. +#' @param heights Used in a call to `patchwork::wrap_plots()`. The relative heights of each row +#' in the plot grid. #' #' See the examples on how to change the color scale. #' @@ -473,7 +477,8 @@ plot_ancestral_states <- function( plot_matrix_phylo <- function( net, at_nodes, tree, host_tree, type = "states", state = 2, repertoire = "fundamental", modules = NULL, module_order = NULL, find_modules = TRUE, - threshold = 0.9, point_size = 3, dodge_width = 0.025, colors = NULL, ladderize = FALSE + threshold = 0.9, point_size = 3, dodge_width = 0.025, colors = NULL, ladderize = FALSE, + widths = c(1,1), heights = c(3,1) ) { # Check inputs if (!is.matrix(net)) stop("`net` should be a matrix.") @@ -533,7 +538,7 @@ plot_matrix_phylo <- function( module_plot + ggplot2::labs(tag = "B"), patchwork::guide_area(), host_plot + ggplot2::labs(tag = "C"), - ncol = 2, widths = c(1, 1), heights = c(3, 1), guides = "collect" + ncol = 2, widths = widths, heights = heights, guides = "collect" ) } diff --git a/man/plot_matrix_phylo.Rd b/man/plot_matrix_phylo.Rd index 165b2a7..1f102de 100644 --- a/man/plot_matrix_phylo.Rd +++ b/man/plot_matrix_phylo.Rd @@ -19,7 +19,9 @@ plot_matrix_phylo( point_size = 3, dodge_width = 0.025, colors = NULL, - ladderize = FALSE + ladderize = FALSE, + widths = c(1, 1), + heights = c(3, 1) ) } \arguments{ @@ -31,9 +33,7 @@ network is represented, otherwise a weighted network is assumed.} \item{tree}{The phylogeny of the symbiont clade (e.g. parasites, herbivores). Object of class \code{phylo}.} -\item{host_tree}{The phylogeny belonging to the hosts. Object of class \code{phylo}. - -See the examples on how to change the color scale.} +\item{host_tree}{The phylogeny belonging to the hosts. Object of class \code{phylo}.} \item{type}{One of \code{'states'} or \code{'repertoires'}. If \code{'states'}, will plot the presence of a state when its posterior probability is higher than \code{threshold}. If \code{'repertoires'}, will plot @@ -74,6 +74,14 @@ as there are modules.} The ancestral states are automatically colored by module. To change what colors are used, you can add color scales to the resulting \code{ggplot}, e.g. \code{ggplot2::scale_color_manual()}.} + +\item{widths}{Used in a call to \code{patchwork::wrap_plots()}. The relative widths of each column +in the plot grid.} + +\item{heights}{Used in a call to \code{patchwork::wrap_plots()}. The relative heights of each row +in the plot grid. + +See the examples on how to change the color scale.} } \value{ An assembly of plots, of class \code{patchwork}. diff --git a/vignettes/articles/evolnets.Rmd b/vignettes/articles/evolnets.Rmd index 00e0983..bd8f424 100644 --- a/vignettes/articles/evolnets.Rmd +++ b/vignettes/articles/evolnets.Rmd @@ -14,13 +14,15 @@ knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning = FALSE, - message = FALSE, - eval = FALSE + message = FALSE ) +``` +```{r, echo=FALSE} options(rmarkdown.html_vignette.check_title = FALSE) ``` + `evolnets` is an R package for summarizing the posterior distribution produced by Bayesian inference of ancestral networks. Currently, the only available approach specific for inference of ecological interactions is based on the model of host-repertoire evolution implemented originally in [RevBayes](https://revbayes.github.io/tutorials/host_rep/host_rep.html) and then in [TreePPL](https://treeppl.org). If you are unfamiliar with this model, check out this [paper in Systematic Biology](https://doi.org/10.1093/sysbio/syaa019) where the model is introduced. Even though the approach used here can be applied to a variety of biological systems, it was developed with host-symbiont interactions in mind, so I'll be using the words *host* and *symbiont*. They could, in principle, be replaced by terms like *host* and *parasite*, *resource* and *consumer*, *lower trophic level* and *higher trophic level*, or any other two groups of nodes that form a bipartite network. @@ -136,7 +138,9 @@ pp_at_deep_nodes[, c("Fabaceae", "Capparaceae", "Rosaceae"), ] We can see that Fabaceae was most likely an ancestral hosts for Pieridae butterflies. Capparaceae might have been a host at the origin of Pieridae, but most certainly starting at node "Index_128". On the other hand, the probability that these butterflies used Rosaceae as a host is very small. -Instead of just looking at numbers, we can plot the ancestral hosts for all internal nodes in the symbiont tree. For that, we have to choose a probability threshold over which to plot (default is 0.9). Ancestral states are colored by modules in the extant network, so we have to either define the modules first or let it be done within the plotting function. Let's plot both trees, the interaction matrix, and the ancestral states at once, like so: +Instead of just looking at numbers, we can plot the ancestral hosts for all internal nodes in the symbiont tree. For that, we have to choose a probability threshold over which to plot (default is 0.9). Ancestral states are colored by modules in the extant network, so we have to either define the modules first or let it be done within the plotting function. As this is a stochastic process, the modules will change a little every time the `plot_matrix_phylo()` is called. + +Let's plot both trees, the interaction matrix, and the ancestral states at once, like so: ```{r} at_nodes <- posterior_at_nodes(history, tree, host_tree) @@ -162,14 +166,14 @@ The main goal of `evolnets` is to reconstruct the evolution of networks. We can `posterior_at_ages( )` finds the symbiont lineages that were extant at given time points in the past and calculates the posterior probability for interactions between these symbionts and each host based on samples from the MCMC. The first element of `at_ages` contains the sampled networks and the second, the posterior probabilities. -```{r} +```{r, eval = FALSE} ages <- c(60, 50, 40, 0) at_ages <- posterior_at_ages(history, ages, tree, host_tree) ``` Then, we can make different interaction matrices based on two things: the minimum posterior probability for an interaction to be included in the network, and whether we want a binary or a weighted network. -```{r} +```{r, eval = FALSE} weighted_nets_50 <- get_summary_networks(at_ages, threshold = 0.5, weighted = TRUE) binary_nets_90 <- get_summary_networks(at_ages, threshold = 0.9, weighted = FALSE) @@ -183,13 +187,13 @@ binary_nets_90 <- get_summary_networks(at_ages, threshold = 0.9, weighted = FALS ### Sampled networks -```{r} +```{r, eval = FALSE} samples_at_ages <- get_sampled_networks(at_ages) ``` Then, we can calculate network properties for each sampled network. -```{r} +```{r, eval = FALSE} # calculate posterior distribution of nestedness Nz <- index_at_ages_samples(samples_at_ages, index = "NODF", nnull = 10)