diff --git a/DESCRIPTION b/DESCRIPTION index 8807b46..d621e6c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: ggtree Type: Package Title: an R package for visualization of tree and annotation data -Version: 3.13.1 +Version: 3.13.2 Authors@R: c( person("Guangchuang", "Yu", email = "guangchuangyu@gmail.com", role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0002-6485-8781")), @@ -31,7 +31,7 @@ Imports: methods, purrr, rlang, - ggfun (>= 0.1.5), + ggfun (>= 0.1.7), yulab.utils (>= 0.1.6), tidyr, tidytree (>= 0.4.5), diff --git a/NAMESPACE b/NAMESPACE index 4522004..9908cf8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -178,6 +178,9 @@ importFrom(dplyr,select) importFrom(ggfun,"%<+%") importFrom(ggfun,get_aes_var) importFrom(ggfun,identify) +importFrom(ggfun,td_filter) +importFrom(ggfun,td_mutate) +importFrom(ggfun,td_unnest) importFrom(ggplot2,Geom) importFrom(ggplot2,GeomLabel) importFrom(ggplot2,GeomPoint) diff --git a/NEWS.md b/NEWS.md index 83f64eb..60201f0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,6 +22,10 @@ --> +# ggtree 3.13.2 + ++ mv `td_filter()`, `td_unnest()` and `td_mutate()` to 'ggfun' package (2024-10-24, Thu) + # ggtree 3.13.1 + layout argument can be a function to re-calculate the coord of nodes (2024-07-27, Sat, #622) diff --git a/R/reexports.R b/R/reexports.R index a72a550..a5ecade 100644 --- a/R/reexports.R +++ b/R/reexports.R @@ -1,58 +1,71 @@ -##' @importFrom ggfun %<+% -##' @export -##' @examples -##' nwk <- system.file("extdata", "sample.nwk", package="treeio") -##' tree <- read.tree(nwk) -##' p <- ggtree(tree) -##' dd <- data.frame(taxa=LETTERS[1:13], -##' place=c(rep("GZ", 5), rep("HK", 3), rep("CZ", 4), NA), -##' value=round(abs(rnorm(13, mean=70, sd=10)), digits=1)) -##' row.names(dd) <- NULL -##' p %<+% dd + geom_text(aes(color=place, label=label), hjust=-0.5) +#' @importFrom ggfun %<+% +#' @export +#' @examples +#' nwk <- system.file("extdata", "sample.nwk", package="treeio") +#' tree <- read.tree(nwk) +#' p <- ggtree(tree) +#' dd <- data.frame(taxa=LETTERS[1:13], +#' place=c(rep("GZ", 5), rep("HK", 3), rep("CZ", 4), NA), +#' value=round(abs(rnorm(13, mean=70, sd=10)), digits=1)) +#' row.names(dd) <- NULL +#' p %<+% dd + geom_text(aes(color=place, label=label), hjust=-0.5) ggfun::`%<+%` -##' @importFrom magrittr %>% -##' @export +#' @importFrom ggfun td_filter +#' @export +ggfun::td_filter + +#' @importFrom ggfun td_mutate +#' @export +ggfun::td_mutate + +#' @importFrom ggfun td_unnest +#' @export +ggfun::td_unnest + + +#' @importFrom magrittr %>% +#' @export magrittr::`%>%` -##' @importFrom ape rtree -##' @export +#' @importFrom ape rtree +#' @export ape::rtree -##' @importFrom ape read.tree -##' @export +#' @importFrom ape read.tree +#' @export ape::read.tree -##' @importFrom tidytree groupOTU -##' @export +#' @importFrom tidytree groupOTU +#' @export tidytree::groupOTU -##' @importFrom tidytree groupClade -##' @export +#' @importFrom tidytree groupClade +#' @export tidytree::groupClade -##' @importFrom tidytree nodeid -##' @export +#' @importFrom tidytree nodeid +#' @export tidytree::nodeid -##' @importFrom tidytree nodelab -##' @export +#' @importFrom tidytree nodelab +#' @export tidytree::nodelab -##' @importFrom tidytree MRCA -##' @export +#' @importFrom tidytree MRCA +#' @export tidytree::MRCA -##' @importFrom dplyr collapse -##' @export +#' @importFrom dplyr collapse +#' @export dplyr::collapse -##' @importFrom ggplot2 fortify -##' @export +#' @importFrom ggplot2 fortify +#' @export ggplot2::fortify -##' @importFrom ggplot2 ggplot -##' @export +#' @importFrom ggplot2 ggplot +#' @export ggplot2::ggplot ##' @importFrom ggplot2 xlim diff --git a/R/treedata-function.R b/R/treedata-function.R deleted file mode 100644 index 83bdcb5..0000000 --- a/R/treedata-function.R +++ /dev/null @@ -1,72 +0,0 @@ -##' filter data for tree annotation layer -##' -##' The 'td_filter()' function returns another function that can be -##' used to subset ggtree() plot data. The function can be passed to the 'data' parameter -##' of geom layer to perform subsetting. All rows that satisy your conditions will be retained. -##' @title td-filter -##' @param ... Expressions that return a logical value. -##' @param .f a function (if any, defaults to NULL) that pre-operate the data -##' @return A function to filter ggtree plot data using conditions defined by '...'. -##' @seealso -##' [filter][dplyr::filter] -##' @author Guangchuang Yu -##' @examples -##' tree <- rtree(30) -##' ## similar to 'ggtree(tree) + geom_tippoint()' -##' ggtree(tree) + geom_point(data = td_filter(isTip)) -##' @references -##' For more detailed demonstration of this function, please refer to chapter 12.5.1 of -##' *Data Integration, Manipulation and Visualization of Phylogenetic Trees* -##' by Guangchuang Yu. -##' @export -td_filter <- function(..., .f = NULL) { - dots <- rlang::quos(...) - function(.data) { - if (!is.null(.f)) .data <- .f(.data) - dplyr::filter(.data, !!!dots) - } -} - -##' flatterns a list-column of data frame -##' -##' The 'td_unnest' function returns another function that can be -##' used to unnest ggtree() plot data. The function can be passed to -##' the 'data' parameter of a geom layer to flattern list-cloumn tree data. -##' @title td-unnest -##' @param cols columns to unnest -##' @param ... additional parameters that pass to tidyr::unnest -##' @param .f a function (if any, defaults to NULL) that pre-operate the data -##' @return A function to unnest ggtree plot data -##' @seealso -##' [unnest][tidyr::unnest] -##' @author Guangchuang Yu -##' @references -##' For demonstration of this function, please refer to chapter 12.5.2 of -##' *Data Integration, Manipulation and Visualization of Phylogenetic Trees* -##' by Guangchuang Yu. -##' @export -td_unnest <- function(cols, ..., .f = NULL) { - function(.data) { - if (!is.null(.f)) .data <- .f(.data) - tidyr::unnest(.data, {{cols}}, ...) - } -} - -##' mutate data for tree annotation layer -##' -##' The 'td_mutate()' function returns another function that can be -##' used to mutate ggtree() plot data. The function can be passed to the 'data' parameter -##' of geom layer to perform adding new variables and preserving existing ones. -##' @title td-mutate -##' @param ... additional parameters that pass to dplyr::mutate -##' @param .f a function (if any, defaults to NULL) that pre-operate the data -##' @return A function to mutate ggtree plot data -##' @seealso -##' [mutate][dplyr::mutate] -##' @export -td_mutate <- function(..., .f=NULL){ - function(.data){ - if (!is.null(.f)) .data <- .f(.data) - dplyr::mutate(.data, ...) - } -} diff --git a/man/reexports.Rd b/man/reexports.Rd index b15f7ab..8cbe8c0 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -4,6 +4,9 @@ \name{reexports} \alias{reexports} \alias{\%<+\%} +\alias{td_filter} +\alias{td_mutate} +\alias{td_unnest} \alias{\%>\%} \alias{rtree} \alias{read.tree} @@ -55,7 +58,7 @@ below to see their documentation. \item{dplyr}{\code{\link[dplyr:compute]{collapse}}} - \item{ggfun}{\code{\link[ggfun:attacher]{\%<+\%}}, \code{\link[ggfun:reexports]{identify}}} + \item{ggfun}{\code{\link[ggfun:attacher]{\%<+\%}}, \code{\link[ggfun:reexports]{identify}}, \code{\link[ggfun]{td_filter}}, \code{\link[ggfun]{td_mutate}}, \code{\link[ggfun]{td_unnest}}} \item{ggplot2}{\code{\link[ggplot2]{aes}}, \code{\link[ggplot2]{fortify}}, \code{\link[ggplot2:geom_text]{geom_label}}, \code{\link[ggplot2]{geom_point}}, \code{\link[ggplot2]{geom_text}}, \code{\link[ggplot2]{ggplot}}, \code{\link[ggplot2]{ggsave}}, \code{\link[ggplot2]{guide_legend}}, \code{\link[ggplot2:element]{margin}}, \code{\link[ggplot2:scale_manual]{scale_color_manual}}, \code{\link[ggplot2:scale_manual]{scale_colour_manual}}, \code{\link[ggplot2:scale_manual]{scale_fill_manual}}, \code{\link[ggplot2:scale_continuous]{scale_x_continuous}}, \code{\link[ggplot2]{theme}}, \code{\link[ggplot2:lims]{xlim}}} diff --git a/man/td_filter.Rd b/man/td_filter.Rd deleted file mode 100644 index e47815b..0000000 --- a/man/td_filter.Rd +++ /dev/null @@ -1,40 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/treedata-function.R -\name{td_filter} -\alias{td_filter} -\title{td-filter} -\usage{ -td_filter(..., .f = NULL) -} -\arguments{ -\item{...}{Expressions that return a logical value.} - -\item{.f}{a function (if any, defaults to NULL) that pre-operate the data} -} -\value{ -A function to filter ggtree plot data using conditions defined by '...'. -} -\description{ -filter data for tree annotation layer -} -\details{ -The 'td_filter()' function returns another function that can be -used to subset ggtree() plot data. The function can be passed to the 'data' parameter -of geom layer to perform subsetting. All rows that satisy your conditions will be retained. -} -\examples{ -tree <- rtree(30) -## similar to 'ggtree(tree) + geom_tippoint()' -ggtree(tree) + geom_point(data = td_filter(isTip)) -} -\references{ -For more detailed demonstration of this function, please refer to chapter 12.5.1 of -\emph{Data Integration, Manipulation and Visualization of Phylogenetic Trees} -\url{http://yulab-smu.top/treedata-book/index.html} by Guangchuang Yu. -} -\seealso{ -\link[dplyr:filter]{filter} -} -\author{ -Guangchuang Yu -} diff --git a/man/td_mutate.Rd b/man/td_mutate.Rd deleted file mode 100644 index 24fe5f5..0000000 --- a/man/td_mutate.Rd +++ /dev/null @@ -1,27 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/treedata-function.R -\name{td_mutate} -\alias{td_mutate} -\title{td-mutate} -\usage{ -td_mutate(..., .f = NULL) -} -\arguments{ -\item{...}{additional parameters that pass to dplyr::mutate} - -\item{.f}{a function (if any, defaults to NULL) that pre-operate the data} -} -\value{ -A function to mutate ggtree plot data -} -\description{ -mutate data for tree annotation layer -} -\details{ -The 'td_mutate()' function returns another function that can be -used to mutate ggtree() plot data. The function can be passed to the 'data' parameter -of geom layer to perform adding new variables and preserving existing ones. -} -\seealso{ -\link[dplyr:mutate]{mutate} -} diff --git a/man/td_unnest.Rd b/man/td_unnest.Rd deleted file mode 100644 index 30c601e..0000000 --- a/man/td_unnest.Rd +++ /dev/null @@ -1,37 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/treedata-function.R -\name{td_unnest} -\alias{td_unnest} -\title{td-unnest} -\usage{ -td_unnest(cols, ..., .f = NULL) -} -\arguments{ -\item{cols}{columns to unnest} - -\item{...}{additional parameters that pass to tidyr::unnest} - -\item{.f}{a function (if any, defaults to NULL) that pre-operate the data} -} -\value{ -A function to unnest ggtree plot data -} -\description{ -flatterns a list-column of data frame -} -\details{ -The 'td_unnest' function returns another function that can be -used to unnest ggtree() plot data. The function can be passed to -the 'data' parameter of a geom layer to flattern list-cloumn tree data. -} -\references{ -For demonstration of this function, please refer to chapter 12.5.2 of -\emph{Data Integration, Manipulation and Visualization of Phylogenetic Trees} -\url{http://yulab-smu.top/treedata-book/index.html} by Guangchuang Yu. -} -\seealso{ -\link[tidyr:unnest]{unnest} -} -\author{ -Guangchuang Yu -}