diff --git a/DESCRIPTION b/DESCRIPTION index 7f6dd2b..6b618a5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -36,7 +36,8 @@ Imports: methods, lifecycle, pbapply, - knitr + knitr, + scales Suggests: testthat (>= 3.0.0), vdiffr (>= 1.0.0), diff --git a/NAMESPACE b/NAMESPACE index 07334d5..fc16bcc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,10 +25,13 @@ export(recolor_phylopic) export(resolve_phylopic) export(rotate_phylopic) export(save_phylopic) +export(scale_height_continuous) +export(scale_width_continuous) importFrom(curl,nslookup) importFrom(ggplot2,Geom) importFrom(ggplot2,aes) importFrom(ggplot2,annotate) +importFrom(ggplot2,continuous_scale) importFrom(ggplot2,coord_equal) importFrom(ggplot2,element_text) importFrom(ggplot2,expansion) @@ -40,6 +43,7 @@ importFrom(ggplot2,layer) importFrom(ggplot2,remove_missing) importFrom(ggplot2,theme) importFrom(ggplot2,theme_void) +importFrom(ggplot2,waiver) importFrom(grDevices,bmp) importFrom(grDevices,col2rgb) importFrom(grDevices,dev.off) @@ -81,6 +85,7 @@ importFrom(pbapply,pblapply) importFrom(png,readPNG) importFrom(rsvg,rsvg_png) importFrom(rsvg,rsvg_svg) +importFrom(scales,pal_area) importFrom(stats,setNames) importFrom(utils,URLdecode) importFrom(utils,URLencode) diff --git a/R/geom_phylopic.R b/R/geom_phylopic.R index acaed08..1673738 100644 --- a/R/geom_phylopic.R +++ b/R/geom_phylopic.R @@ -499,6 +499,40 @@ phylopicGrob <- function(img, x, y, height, width, return(img_grob) } +#' Scales for phylopic heighgt or width +#' +#' `scale_height_continuous()` scales the height of silhouettes whereas +#' `scale_width_continuous()` scales the height of silhouettes. +#' @inheritParams ggplot2::scale_size_continuous +#' @export +#' @importFrom ggplot2 continuous_scale waiver +#' @importFrom scales pal_area +#' @rdname scales +scale_height_continuous <- function(name = waiver(), breaks = waiver(), labels = waiver(), + limits = NULL, range = c(1, 6), + transform = "identity", + trans = lifecycle::deprecated(), + guide = "none") { + continuous_scale("height", palette = scales::pal_area(range), name = name, + breaks = breaks, labels = labels, limits = limits, + transform = transform, trans = trans, guide = guide) +} + +#' @inheritParams ggplot2::scale_size_continuous +#' @export +#' @importFrom ggplot2 continuous_scale waiver +#' @importFrom scales pal_area +#' @rdname scales +scale_width_continuous <- function(name = waiver(), breaks = waiver(), labels = waiver(), + limits = NULL, range = c(1, 6), + transform = "identity", + trans = lifecycle::deprecated(), + guide = "none") { + continuous_scale("width", palette = scales::pal_area(range), name = name, + breaks = breaks, labels = labels, limits = limits, + transform = transform, trans = trans, guide = guide) +} + #' @importFrom grid grobName ggname <- function(prefix, grob) { # copied from ggplot2 diff --git a/man/scales.Rd b/man/scales.Rd new file mode 100644 index 0000000..18a8cc9 --- /dev/null +++ b/man/scales.Rd @@ -0,0 +1,98 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/geom_phylopic.R +\name{scale_height_continuous} +\alias{scale_height_continuous} +\alias{scale_width_continuous} +\title{Scales for phylopic heighgt or width} +\usage{ +scale_height_continuous( + name = waiver(), + breaks = waiver(), + labels = waiver(), + limits = NULL, + range = c(1, 6), + transform = "identity", + trans = lifecycle::deprecated(), + guide = "none" +) + +scale_width_continuous( + name = waiver(), + breaks = waiver(), + labels = waiver(), + limits = NULL, + range = c(1, 6), + transform = "identity", + trans = lifecycle::deprecated(), + guide = "none" +) +} +\arguments{ +\item{name}{The name of the scale. Used as the axis or legend title. If +\code{waiver()}, the default, the name of the scale is taken from the first +mapping used for that aesthetic. If \code{NULL}, the legend title will be +omitted.} + +\item{breaks}{One of: +\itemize{ +\item \code{NULL} for no breaks +\item \code{waiver()} for the default breaks computed by the +\link[scales:new_transform]{transformation object} +\item A numeric vector of positions +\item A function that takes the limits as input and returns breaks +as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}). +Note that for position scales, limits are provided after scale expansion. +Also accepts rlang \link[rlang:as_function]{lambda} function notation. +}} + +\item{labels}{One of: +\itemize{ +\item \code{NULL} for no labels +\item \code{waiver()} for the default labels computed by the +transformation object +\item A character vector giving labels (must be same length as \code{breaks}) +\item An expression vector (must be the same length as breaks). See ?plotmath for details. +\item A function that takes the breaks as input and returns labels +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. +}} + +\item{limits}{One of: +\itemize{ +\item \code{NULL} to use the default scale range +\item A numeric vector of length two providing limits of the scale. +Use \code{NA} to refer to the existing minimum or maximum +\item A function that accepts the existing (automatic) limits and returns +new limits. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. +Note that setting limits on positional scales will \strong{remove} data outside of the limits. +If the purpose is to zoom, use the limit argument in the coordinate system +(see \code{\link[ggplot2:coord_cartesian]{coord_cartesian()}}). +}} + +\item{range}{a numeric vector of length 2 that specifies the minimum and +maximum size of the plotting symbol after transformation.} + +\item{transform}{For continuous scales, the name of a transformation object +or the object itself. Built-in transformations include "asn", "atanh", +"boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", +"logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", +"reverse", "sqrt" and "time". + +A transformation object bundles together a transform, its inverse, +and methods for generating breaks and labels. Transformation objects +are defined in the scales package, and are called \verb{transform_}. If +transformations require arguments, you can call them from the scales +package, e.g. \code{\link[scales:transform_boxcox]{scales::transform_boxcox(p = 2)}}. +You can create your own transformation with \code{\link[scales:new_transform]{scales::new_transform()}}.} + +\item{trans}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Deprecated in favour of +\code{transform}.} + +\item{guide}{A function used to create a guide or its name. See +\code{\link[ggplot2:guides]{guides()}} for more information.} +} +\description{ +\code{scale_height_continuous()} scales the height of silhouettes whereas +\code{scale_width_continuous()} scales the height of silhouettes. +}