Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
120 lines (101 sloc) 4.33 KB
title: "Using Color Palettes"
author: "Rob Gilmore & Shaurita Hutchins"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Color Palettes}
```{r setup, include = FALSE}
collapse = TRUE,
comment = "#>"
library(MicrobiomeR) <- MicrobiomeR:::pkg.private
# Introduction
The color palette workflow created for MicrobiomeR are based on the
`get_color_palette(pal_func = ...)` with a palette function that returns
`grDevices::colorRampPalette(...)` (called), `grDevices::colorRampPalette` (not called),
or a color palette function that returns a character vector. Several palette functions
come built with MicrobiomeR and are used in the plotting functions.
## Getting a Color Palette
Getting a color palette is simple. You can toggle the palette preview with `display=FALSE`.
```{r message=FALSE, warning=FALSE}
analyzed_silva <- as_MicrobiomeR_format(MicrobiomeR::raw_silva_2, "analyzed_format")
data <- analyzed_silva$data$stats_tax_data$Phylum
data_len <- length(unique(data))
custom_pal <- get_color_palette(color_no=data_len)
## Scico
We have built in support for the [scico]( package.
```{r message=FALSE, warning=FALSE}
basic_pal <- scico_palette(scico_palette="hawaii")(25)
pie(rep(1, length(basic_pal)), col=basic_pal)
better_pal <- get_color_palette(pal_func = scico_palette, color_no = 25)
## Viridis
We also provide support for the [viridis]( package.
Typically, you'd use the viridis palette like we do below.
```{r message=FALSE, warning=FALSE}
basic_pal <- viridis::viridis(n=25, option="magma")
pie(rep(1, length(basic_pal)), col=basic_pal)
With `MicrobiomeR`, the viridis palette can be optimized.
```{r message=FALSE, warning=FALSE}
better_pal <- get_color_palette(pal_func = viridis_palette, color_no = 25)
pie(rep(1, length(better_pal)), col=better_pal)
## Combining Color Palettes
Here is a useful functionality. Combining color palettes can help with situations
where you need something more custom. This can be especially helpful if you are using
more than 20 colors. The `combination_palette` function can take a dynamic number of
arguments, must be named and the value must be a `list`. The list members can contain members with the following names:
> You can use any name for your arguments, but the values must be a named list.
palette: This is a palette function that returns a vector of colors.
args: This is another named list used for the palette function parameters.
range: This is a range (1:10) used to subset the color palette vector.
rev: This is a logical (TRUE/FALSE)used to reverse the color palette.
```{r message=FALSE, warning=FALSE}
# This is the code for MicrobiomeR::viridis_magma_palette, but
# it's also a useful example for understanding how this works.
viridis_magma_palette <- function(viridis_number = 800,
viridis_range = 300:viridis_number,
viridis_rev = TRUE,
magma_number = 500,
magma_range = 0:magma_number,
magma_rev = FALSE,
...) {
# Get the arguments for both functions
if (!missing(...)){
v_args = list(n=viridis_number, ...)
m_args = list(n=magma_number, ...)
} else {
v_args = list(n=viridis_number)
m_args = list(n=magma_number)
# Get combo palette
crp <- combination_palette(viridis =
list(palette = viridis::viridis,
args = v_args,
range = viridis_range,
rev = viridis_rev),
magma =
list(palette = viridis::magma,
args = m_args,
range = magma_range,
rev = magma_rev)
combo_palette <- get_color_palette(pal_func = viridis_magma_palette)
You can’t perform that action at this time.