diff --git a/DESCRIPTION b/DESCRIPTION index 09684ee..b64af2b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,7 +9,10 @@ Authors@R: c( Description: Maps one of the viridis colour palettes, or a user-specified palette to values. Viridis colour maps are created by Stéfan van der Walt and Nathaniel Smith. They were set as the default palette for the 'Python' 'Matplotlib' library, - introduced at SciPy 2015 conference . + introduced at SciPy 2015 conference . + Other palettes available in this library have been derived from + 'RColorBrewer' and + 'colorspace' packages. License: GPL-3 Encoding: UTF-8 LazyData: true diff --git a/R/palettes.R b/R/palettes.R index 0b9e358..d33dc09 100644 --- a/R/palettes.R +++ b/R/palettes.R @@ -3,24 +3,102 @@ #' #' List the available colour palettes. #' +#' @param colours vector of source colour palettes to return, one or many of +#' "viridis","rcolorbrewer","grdevices","colorspace","colorramp". NULL will reutrn all palettes. +#' +#' @details +#' The palettes avaialble in \code{colourvalues} have been derived from those avaialble in +#' the libraries +#' +#' \itemize{ +#' \item{viridis} +#' \item{RColorBrewer} +#' \item{grDevices} +#' \item{colorspaces} +#' \item{colorRamp} +#' } +#' #' @examples #' colour_palettes() +#' colour_palettes( "viridis" ) +#' colour_palettes( colours = c("rcolorbrewer","grdevices") ) #' #' @export -colour_palettes <- function() { +colour_palettes <- function( colours = NULL ) { + if( is.null( colours ) ) colours <- c("viridis","rcolorbrewer","grdevices","colorspace","colorramp") + colours <- tolower(colours) + + return( + c( + if( "viridis" %in% colours ) { viridis_palettes() } + , if( "rcolorbrewer" %in% colours ) { c( rcolorbrewer_seq_palettes(), rcolorbrewer_div_palettes() ) } + , if( "grdevices" %in% colours ) { grdevices_palettes() } + , if( "colorspace" %in% colours ) { c( colorspace_seq_palettes(), colorspace_div_palettes() ) } + , if( "colorramp" %in% colours ) { colorramp_palettes() } + ) + ) +} + +viridis_palettes <- function() { + return( + c("viridis", "cividis", "magma", "inferno", "plasma") + ) +} + +rcolorbrewer_seq_palettes <- function() { + return( + c( + "ylorrd", + "ylorbr", "ylgnbu", "ylgn", "reds", "rdpu", "purples", "purd", + "pubugn", "pubu", "orrd", "oranges", "greys", "greens", "gnbu", + "bupu", "bugn", "blues" + ) + ) +} + +rcolorbrewer_div_palettes <- function() { + return( + c( + "spectral", "rdylgn", "rdylbu", "rdgy", + "rdbu", "puor", "prgn", "piyg", "brbg" + ) + ) +} + +grdevices_palettes <- function() { return( - c("viridis", "cividis", "magma", "inferno", "plasma", "blue2green", - "blue2red", "blue2yellow", "blues", "brbg", "bugn", "bupu", "cm", - "cyan2yellow", "diverge_hcl", "diverge_hsv", "gnbu", "green2red", - "greens", "greys", "heat", "heat_hcl", "magenta2green", "matlab_like", - "matlab_like2", "oranges", "orrd", "piyg", "prgn", "pubu", "pubugn", - "puor", "purd", "purples", "rainbow", "rainbow_hcl", "rdbu", - "rdgy", "rdpu", "rdylbu", "rdylgn", "reds", "sequential_hcl", - "spectral", "terrain", "terrain_hcl", "topo", "ygobb", "ylgn", - "ylgnbu", "ylorbr", "ylorrd") + c( + "terrain", "topo", "heat", + "cm", "rainbow" + ) ) } +colorspace_seq_palettes <- function() { + return( + c( + "terrain_hcl", "heat_hcl", "sequential_hcl", + "rainbow_hcl" + ) + ) +} + +colorspace_div_palettes <- function() { + return( + c("diverge_hcl", "diverge_hsv") + ) +} + +colorramp_palettes <- function() { + return( + c( + "ygobb", "matlab_like2", "matlab_like", "magenta2green", + "cyan2yellow", "blue2yellow", "green2red", "blue2green", "blue2red" + ) + ) +} + + #' @rdname colour_palettes #' @export color_palettes <- colour_palettes @@ -28,7 +106,7 @@ color_palettes <- colour_palettes #' Show Colours #' -#' Plots all the selected colours +#' Plots all the selected colours. See \link{colour_palettes} for avaialble colours. #' #' @param colours vector of colour palettes #' @@ -38,7 +116,9 @@ color_palettes <- colour_palettes #' show_colours() #' #' ## view a selection of colour palettes -#' show_colours( colours = colour_palettes()[1:10] ) +#' show_colours( colours = colour_palettes( c("viridis", "grdevices") ) ) +#' +#' #' #' @export show_colours <- function( colours = colour_palettes() ) { diff --git a/README.Rmd b/README.Rmd index 5f8304f..e159228 100644 --- a/README.Rmd +++ b/README.Rmd @@ -244,7 +244,7 @@ library(ggplot2) library(scales) library(viridisLite) -n <- 1e6 +n <- 1e7 df <- data.frame(x = rnorm(n = n)) m <- microbenchmark( @@ -266,7 +266,7 @@ library(ggplot2) library(scales) library(viridisLite) -n <- 5e5 +n <- 1e6 x <- sample(x = letters, size = n, replace = TRUE) df <- data.frame(x = x) diff --git a/README.md b/README.md index c1f3667..0794cdb 100644 --- a/README.md +++ b/README.md @@ -244,11 +244,11 @@ summary ``` r colour_values(rnorm(n = 10), n_summaries = 3, digits = 2) # $colours -# [1] "#49C16EFF" "#31668EFF" "#20938CFF" "#54C568FF" "#FDE725FF" -# [6] "#440154FF" "#F6E61FFF" "#47C06FFF" "#1F998AFF" "#38578CFF" +# [1] "#DAE319FF" "#F8E621FF" "#1F978BFF" "#84D44BFF" "#1E9B8AFF" +# [6] "#FDE725FF" "#31678EFF" "#2F6D8EFF" "#A6DB35FF" "#440154FF" # # $summary_values -# [1] "-1.55" "0.11" "1.77" +# [1] "-2.31" "-0.87" "0.57" # # $summary_colours # [1] "#440154FF" "#21908CFF" "#FDE725FF" @@ -298,26 +298,28 @@ values, and their associated colours ``` r colour_values(sample(letters, size = 50, replace = T), summary = T) # $colours -# [1] "#481567FF" "#453781FF" "#73D055FF" "#404788FF" "#29AF7FFF" -# [6] "#238A8DFF" "#29AF7FFF" "#1F968BFF" "#95D840FF" "#482677FF" -# [11] "#3CBB75FF" "#FDE725FF" "#238A8DFF" "#39568CFF" "#95D840FF" -# [16] "#39568CFF" "#DCE319FF" "#73D055FF" "#440154FF" "#FDE725FF" -# [21] "#238A8DFF" "#1F968BFF" "#B8DE29FF" "#39568CFF" "#95D840FF" -# [26] "#440154FF" "#95D840FF" "#33638DFF" "#404788FF" "#453781FF" -# [31] "#287D8EFF" "#33638DFF" "#FDE725FF" "#2D708EFF" "#55C667FF" -# [36] "#2D708EFF" "#404788FF" "#482677FF" "#2D708EFF" "#B8DE29FF" -# [41] "#95D840FF" "#1F968BFF" "#20A387FF" "#20A387FF" "#482677FF" -# [46] "#287D8EFF" "#453781FF" "#404788FF" "#39568CFF" "#20A387FF" +# [1] "#C9E01FFF" "#1F9E88FF" "#22A884FF" "#E4E418FF" "#1F9E88FF" +# [6] "#3ABA76FF" "#3ABA76FF" "#2E6E8EFF" "#26828EFF" "#1F958BFF" +# [11] "#22A884FF" "#2E6E8EFF" "#C9E01FFF" "#E4E418FF" "#E4E418FF" +# [16] "#471063FF" "#472B7AFF" "#7AD151FF" "#FDE725FF" "#26828EFF" +# [21] "#1F958BFF" "#228B8DFF" "#4DC36CFF" "#375A8CFF" "#481E70FF" +# [26] "#440154FF" "#7AD151FF" "#453882FF" "#7AD151FF" "#7AD151FF" +# [31] "#FDE725FF" "#4DC36CFF" "#93D741FF" "#3C4F8BFF" "#2E6E8EFF" +# [36] "#32648EFF" "#3C4F8BFF" "#32648EFF" "#2CB17EFF" "#62CA5FFF" +# [41] "#3C4F8BFF" "#228B8DFF" "#414487FF" "#2A788EFF" "#453882FF" +# [46] "#AEDC30FF" "#AEDC30FF" "#375A8CFF" "#26828EFF" "#471063FF" # # $summary_values -# [1] "a" "b" "e" "f" "g" "h" "i" "j" "l" "m" "n" "o" "q" "r" "s" "u" "w" -# [18] "x" "y" "z" +# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" +# [18] "r" "s" "t" "u" "v" "w" "x" "y" "z" # # $summary_colours -# [1] "#440154FF" "#481567FF" "#482677FF" "#453781FF" "#404788FF" -# [6] "#39568CFF" "#33638DFF" "#2D708EFF" "#287D8EFF" "#238A8DFF" -# [11] "#1F968BFF" "#20A387FF" "#29AF7FFF" "#3CBB75FF" "#55C667FF" -# [16] "#73D055FF" "#95D840FF" "#B8DE29FF" "#DCE319FF" "#FDE725FF" +# [1] "#440154FF" "#471063FF" "#481E70FF" "#472B7AFF" "#453882FF" +# [6] "#414487FF" "#3C4F8BFF" "#375A8CFF" "#32648EFF" "#2E6E8EFF" +# [11] "#2A788EFF" "#26828EFF" "#228B8DFF" "#1F958BFF" "#1F9E88FF" +# [16] "#22A884FF" "#2CB17EFF" "#3ABA76FF" "#4DC36CFF" "#62CA5FFF" +# [21] "#7AD151FF" "#93D741FF" "#AEDC30FF" "#C9E01FFF" "#E4E418FF" +# [26] "#FDE725FF" ``` ----- @@ -332,7 +334,7 @@ library(ggplot2) library(scales) library(viridisLite) -n <- 1e6 +n <- 1e7 df <- data.frame(x = rnorm(n = n)) m <- microbenchmark( @@ -341,13 +343,10 @@ m <- microbenchmark( times = 25 ) m -# Unit: milliseconds -# expr min lq mean median uq max -# colourvalues 413.7846 465.2459 667.549 533.3406 775.9402 1768.377 -# scales 865.6236 985.3206 1476.348 1357.1543 2078.2787 2857.309 -# neval -# 25 -# 25 +# Unit: seconds +# expr min lq mean median uq max neval +# colourvalues 1.648931 1.677334 1.700990 1.694654 1.709067 1.823521 25 +# scales 2.824650 2.908006 2.968315 2.945947 3.013193 3.338338 25 autoplot(m) # Coordinate system already present. Adding new coordinate system, which will replace the existing one. @@ -363,7 +362,7 @@ library(ggplot2) library(scales) library(viridisLite) -n <- 5e5 +n <- 1e6 x <- sample(x = letters, size = n, replace = TRUE) df <- data.frame(x = x) @@ -375,8 +374,8 @@ m <- microbenchmark( m # Unit: milliseconds # expr min lq mean median uq max neval -# colourvalues 219.6011 251.1079 281.4803 257.8884 268.8997 457.7094 25 -# scales 392.0473 418.4014 536.5625 453.4664 566.0392 999.5088 25 +# colourvalues 188.1557 190.9667 194.2682 192.4433 194.4276 244.0605 25 +# scales 300.0350 314.6473 323.1116 316.2003 318.6083 409.6637 25 autoplot(m) # Coordinate system already present. Adding new coordinate system, which will replace the existing one. diff --git a/man/colour_palettes.Rd b/man/colour_palettes.Rd index 7745bf7..5801cf8 100644 --- a/man/colour_palettes.Rd +++ b/man/colour_palettes.Rd @@ -5,14 +5,32 @@ \alias{color_palettes} \title{Colour Palettes} \usage{ -colour_palettes() +colour_palettes(colours = NULL) -color_palettes() +color_palettes(colours = NULL) +} +\arguments{ +\item{colours}{vector of source colour palettes to return, one or many of +"viridis","rcolorbrewer","grdevices","colorspace","colorramp". NULL will reutrn all palettes.} } \description{ List the available colour palettes. } +\details{ +The palettes avaialble in \code{colourvalues} have been derived from those avaialble in +the libraries + +\itemize{ + \item{viridis} + \item{RColorBrewer} + \item{grDevices} + \item{colorspaces} + \item{colorRamp} +} +} \examples{ colour_palettes() +colour_palettes( "viridis" ) +colour_palettes( colours = c("rcolorbrewer","grdevices") ) } diff --git a/man/figures/README-unnamed-chunk-16-1.png b/man/figures/README-unnamed-chunk-16-1.png index c046119..9387a40 100644 Binary files a/man/figures/README-unnamed-chunk-16-1.png and b/man/figures/README-unnamed-chunk-16-1.png differ diff --git a/man/figures/README-unnamed-chunk-17-1.png b/man/figures/README-unnamed-chunk-17-1.png index 3076c28..691125c 100644 Binary files a/man/figures/README-unnamed-chunk-17-1.png and b/man/figures/README-unnamed-chunk-17-1.png differ diff --git a/man/figures/README-unnamed-chunk-5-1.png b/man/figures/README-unnamed-chunk-5-1.png index 7bd7338..04a1141 100644 Binary files a/man/figures/README-unnamed-chunk-5-1.png and b/man/figures/README-unnamed-chunk-5-1.png differ diff --git a/man/figures/README-unnamed-chunk-6-1.png b/man/figures/README-unnamed-chunk-6-1.png index c58da52..4f1ced3 100644 Binary files a/man/figures/README-unnamed-chunk-6-1.png and b/man/figures/README-unnamed-chunk-6-1.png differ diff --git a/man/show_colours.Rd b/man/show_colours.Rd index dcdce87..87ed9fa 100644 --- a/man/show_colours.Rd +++ b/man/show_colours.Rd @@ -10,7 +10,7 @@ show_colours(colours = colour_palettes()) \item{colours}{vector of colour palettes} } \description{ -Plots all the selected colours +Plots all the selected colours. See \link{colour_palettes} for avaialble colours. } \examples{ @@ -18,6 +18,8 @@ Plots all the selected colours show_colours() ## view a selection of colour palettes -show_colours( colours = colour_palettes()[1:10] ) +show_colours( colours = colour_palettes( c("viridis", "grdevices") ) ) + + }