#' Plot trait distribution through time
#'
#' A forked version of the of [`comrad::plot_comm_traits_evolution()`](https://github.com/TheoPannetier/comrad/blob/master/R/plot_comm_trait_evolution.R).
#' The main difference is the (faster) use of rectangles instead of hexes to
#' represent occupied areas of the trait distribution, and more straightforwards
#' plotting options.
#'
#' @param comsie_tbl a tibble, e.g. output of [read_comsie_tbl()]
#' @param fill_by How should rectangles be coloured? either "species" or "clade".
#' @param xlim length 2 num vector to pass to ggplot's `coord_cartesian()`
#' @param ylim length 2 num vector to pass to ggplot's `coord_cartesian()`
#' @param binwidths length 2 num vector, horizontal and vertical widths of the
#' rectangles.
#' @param alpha the ggplot parameter
#'
#' @export
plot_zt <- function(comsie_tbl, fill_by = "clade", xlim = c(0, max(comsie_tbl$t)), ylim = c(-7.5, 7.5), binwidths = c(100, 0.01), alpha = 1) {
if (fill_by == "species") {
comsie_tbl <- comsie_tbl %>% dplyr::rename("fill_var" = species)
} else if (fill_by == "clade"){
if (names(comsie_tbl)[5] == "founder") {
comsie_tbl <- comsie_tbl %>% dplyr::rename("fill_var" = founder)
} else if (names(comsie_tbl)[5] == "mainland_sp") {
comsie_tbl <- comsie_tbl %>% dplyr::rename("fill_var" = mainland_sp)
} else {
stop("Couldn't find clade column")
}
} else {
stop("\"fill_by\" must be either species or clade.")
}
colour_names <- substr(unique(comsie_tbl$fill_var), 1, 7)
names(colour_names) <- unique(comsie_tbl$fill_var)
gg <- comsie_tbl %>%
ggplot2::ggplot(ggplot2::aes(x = t, y = z)) +
ggplot2::scale_y_continuous(minor_breaks = seq(-10, 10, 0.1)) +
ggplot2::labs(x = "Generation", y = "Trait") +
ggplot2::geom_bin2d(
ggplot2::aes(fill = fill_var),
alpha = alpha,
binwidth = binwidths,
size = 0.01,
color = "black",
show.legend = FALSE
) +
ggplot2::scale_fill_manual(
values = colour_names
) +
ggplot2::coord_cartesian(xlim = xlim, ylim = ylim) +
ggplot2::theme_bw()
return(gg)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.