From 7b382e60763c03779fc2a4be9dd5d1e56f80cf1a Mon Sep 17 00:00:00 2001 From: levisc8 Date: Thu, 29 Mar 2018 11:52:17 +0200 Subject: [PATCH] Remove unnecessary function argument, add example --- R/base_figure.R | 40 +++++++++++++++++++++++++++++++------- man/whittaker_base_plot.Rd | 29 +++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/R/base_figure.R b/R/base_figure.R index f1c258f..af0b28c 100644 --- a/R/base_figure.R +++ b/R/base_figure.R @@ -2,14 +2,13 @@ #' #' @description Creates the Whittaker biome figure from the vignette example. #' This can be modified by passing additional \code{\link{ggplot2}} style -#' arguments to it +#' arguments to it. #' @param color_palette A named or unnamed vector of length 9 that contains either #' color names or values. If named, the names should correspond to biome names in the #' \code{Whittaker_biomes} data object. See details for additional information. #' The default is to use the colors from Figure 5.5 in Ricklefs, R. E. (2008), #' \emph{The economy of nature} (Chapter 5, Biological Communities, The biome concept). #' If the vector is not named, the function will insert the names automatically -#' @param extra_features Additional arguments to customize the Whittaker biome plot #' #' @return An object of class \code{gg} and \code{ggplot}. #' @@ -30,14 +29,35 @@ #' \item{\code{Woodland/shrubland}} #' } #' +#' If the vector is unnamed, the names from \emph{Ricklefs (2008)} will be +#' inserted automatically. +#' +#' Add additional features (e.g. \code{theme()} elements) using normal +#' \code{ggplot2} syntax. See examples. +#' +#' @examples +#' +#' library(ggplot2) +#' # Create the base plot +#' +#' whittaker_base_plot() +#' +#' # move the legend to top left corner, add border box, +#' # and adjust the background fill and grid. +#' +#' whittaker_base_plot() + +#' theme(legend.position = c(0.15, 0.75), +#' panel.background = element_blank(), +#' panel.grid.major = element_line(gray(0.7)), +#' panel.border = element_rect(fill = NA)) +#' #' @author Sam Levin, Valentin Stefan #' #' @import ggplot2 #' @importFrom utils data #' @export -whittaker_base_plot <- function(color_palette = NULL, - extra_features = NULL) { +whittaker_base_plot <- function(color_palette = NULL) { utils::data('Whittaker_biomes', envir = environment()) utils::data("Ricklefs_colors", envir = environment()) @@ -46,8 +66,15 @@ whittaker_base_plot <- function(color_palette = NULL, xlabel <- expression("Temperature " ( degree*C)) if(is.null(color_palette)) { color_palette <- Ricklefs_colors - } else if(is.null(names(color_palette))) { + } else if(is.null(names(color_palette)) | + any(is.na(names(color_palette)))) { + # ^^second condition throws warning when names aren't specified. + # consider changing + names(color_palette) <- names(Ricklefs_colors) + + message("Names for 'color_palette' either not specified or too few", + " were specified. Using names from 'Ricklefs_colors'.") } plt <- ggplot2::ggplot() + @@ -65,8 +92,7 @@ whittaker_base_plot <- function(color_palette = NULL, labels = names(color_palette), values = color_palette) + ggplot2::scale_x_continuous(xlabel) + - ggplot2::scale_y_continuous('Precipitation (cm)') + - extra_features + ggplot2::scale_y_continuous('Precipitation (cm)') return(plt) diff --git a/man/whittaker_base_plot.Rd b/man/whittaker_base_plot.Rd index d95308a..523490c 100644 --- a/man/whittaker_base_plot.Rd +++ b/man/whittaker_base_plot.Rd @@ -4,7 +4,7 @@ \alias{whittaker_base_plot} \title{Create the Whittaker biome base figure} \usage{ -whittaker_base_plot(color_palette = NULL, extra_features = NULL) +whittaker_base_plot(color_palette = NULL) } \arguments{ \item{color_palette}{A named or unnamed vector of length 9 that contains either @@ -13,8 +13,6 @@ color names or values. If named, the names should correspond to biome names in t The default is to use the colors from Figure 5.5 in Ricklefs, R. E. (2008), \emph{The economy of nature} (Chapter 5, Biological Communities, The biome concept). If the vector is not named, the function will insert the names automatically} - -\item{extra_features}{Additional arguments to customize the Whittaker biome plot} } \value{ An object of class \code{gg} and \code{ggplot}. @@ -22,7 +20,7 @@ An object of class \code{gg} and \code{ggplot}. \description{ Creates the Whittaker biome figure from the vignette example. This can be modified by passing additional \code{\link{ggplot2}} style -arguments to it +arguments to it. } \details{ To specify your own color palette, create a named vector @@ -41,6 +39,29 @@ character (e.g. 'red' or '#C1E1DD'). The names for each biome are as follows: \item{\code{Temperate grassland/desert}} \item{\code{Woodland/shrubland}} } + +If the vector is unnamed, the names from \emph{Ricklefs (2008)} will be +inserted automatically. + +Add additional features (e.g. \code{theme()} elements) using normal +\code{ggplot2} syntax. See examples. +} +\examples{ + +library(ggplot2) +# Create the base plot + +whittaker_base_plot() + +# move the legend to top left corner, add border box, +# and adjust the background fill and grid. + +whittaker_base_plot() + + theme(legend.position = c(0.15, 0.75), + panel.background = element_blank(), + panel.grid.major = element_line(gray(0.7)), + panel.border = element_rect(fill = NA)) + } \author{ Sam Levin, Valentin Stefan