knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = TRUE,
  message = FALSE, 
  warning = FALSE
)
library(fishpals)
library(ggplot2)

Introduction

This vignette presents examples of all the themes included in the package.

The fishpals package provides fish-inspired color palettes and some lightweight ggplot themes that we hope will be especially useful to scientists when preparing reports, presentations, and publications.

1. Installing and loading fishpals into R


fishpals is a pretty trivial package, so it's unlikely it will ever be submitted to CRAN. You can install the fishpals package using the install_github function from either the remotes or devtools packages:

devtools::install_github("fishsciences/fishpals", build_vignettes = TRUE) 
library(fishpals)

2. Convenience functions

To see all the palettes included in the fishpals package, use getpals():

getpals()

To view a particular palette, call it by name with viewpals()

viewpals("surgewrasse")

To see a full list of all the colors in each palette, use get_fishpals_cols():

get_fishpals_cols()

3. The scale_*_fishpals() functions

fishpals is designed to integrate with ggplot2 code, and so provides two ggplot2 scale functions for displaying its custom color palettes. To use them, map a variable to a color aesthetic and call the appropriate scale function: scale_fill_fishpals() or scale_color_fishpals():

ggplot(warpbreaks) +
   geom_boxplot(aes(x = wool, 
                    y = breaks, 
                    color = wool,
                    fill = wool),
                alpha = 0.5,
                width = 0.5,
                show.legend = FALSE) +
  facet_wrap(~tension,
             labeller = labeller(tension = 
                                   c("L" = "Low",
                                     "M" = "Medium",
                                     "H" = "High")
                              )) +
  scale_color_fishpals(discrete = TRUE) +
  scale_fill_fishpals(discrete = TRUE) +
  labs(x = "Wool type",
       y = "Number of breaks",
       title = "Breaks ~ wool type + tension level")

The default color palette is "cfs". To use a different one, just call it by name:

ggplot(warpbreaks, aes(x = wool, y = breaks)) +
  geom_jitter(aes(color = tension), 
              size = 2.5, 
              width = 0.15) +
  scale_color_fishpals("adultchinook")

4. Lightweight Themes

theme_report()

This theme is designed to have good properties when knitting to pdf or to Microsoft Word. These properties include:

  1. Default size 11 font, with size 11 for axis titles and size 14 for chart titles.
  2. Default Border around the plot margins and the inner panel.
  3. Close-ish spacing of facets to fit on a vertical page.
  4. Default legend position of bottom, horizontal orientation.
ggplot(mpg) +
  geom_point(aes(x = displ, y = hwy)) +
  theme_report() +
  labs(y = "Miles per gallon",
       x = "Engine displacement (L)",
       title = "Highway mpg ~ Engine displacement")

ggplot(economics_long, aes(x = date, y = value01)) +
  geom_line(aes(color = variable),
             size = 0.5) +
  theme_report() +
  scale_color_fishpals() +
  guides(color = guide_legend(title = NULL)) +
  labs(x = "Date",
       y = "Value index",
       title = "Scaled economic indices ~ year") 

ggplot(warpbreaks) +
   geom_boxplot(aes(x = wool, 
                    y = breaks, 
                    color = wool,
                    fill = wool),
                alpha = 0.5,
                width = 0.5,
                show.legend = FALSE) +
  facet_wrap(~tension,
             labeller = labeller(tension = 
                                   c("L" = "Low",
                                     "M" = "Medium",
                                     "H" = "High")
                              )) +
  scale_color_fishpals(discrete = TRUE) +
  scale_fill_fishpals(discrete = TRUE) +
  theme_report(inner_border = FALSE) +
  labs(x = "Wool type",
       y = "Number of breaks",
       title = "Breaks ~ wool type + tension level")

theme_pres()

This theme is designed to have good default properties for presentations. These properties include:

  1. Default size 14 font
  2. Default transparencies for plot.background element_rect()s, which will correspond to a dark theme when you save with ggsave(..., bg = "transparent").
  3. Minimalist design conducive to slide presentations.
  4. Widely-spaced facet panels
ggplot(mpg) +
  geom_point(aes(x = displ, y = hwy)) +
  theme_pres() +
  labs(y = "Miles per gallon",
       x = "Engine displacement (L)",
       title = "Highway mpg ~ Engine displacement")


ggplot(economics_long, aes(x = date, y = value01)) +
  geom_line(aes(color = variable),
             size = 0.5) +
  theme_pres() +
  scale_color_fishpals() +
  guides(color = guide_legend(title = NULL)) +
  labs(x = "Date",
       y = "Value index",
       title = "Scaled economic indices ~ year") 



ggplot(warpbreaks) +
   geom_boxplot(aes(x = wool, 
                    y = breaks, 
                    color = wool,
                    fill = wool),
                alpha = 0.5,
                width = 0.25,
                show.legend = FALSE) +
  facet_wrap(~tension,
             labeller = labeller(tension = 
                                   c("L" = "Low",
                                     "M" = "Medium",
                                     "H" = "High")
                              ),
             ncol = 1) +
  scale_color_fishpals(discrete = TRUE) +
  scale_fill_fishpals(discrete = TRUE) +
  theme_pres() +
  labs(x = "Wool type",
       y = "Number of breaks",
       title = "Breaks ~ wool type + tension level")

theme_pub() has some default font sizes and axis options often used in publications:

ggplot(economics_long, aes(x = date, y = value01)) +
  geom_line(aes(color = variable),
             size = 0.5) +
  theme_pub() +
  scale_color_brewer(palette = 6) + # using color brewer black and white palette for demonstration
  guides(color = guide_legend(title = NULL)) +
  labs(x = "Date",
       y = "Value index",
       title = "Scaled economic indices ~ year") +
  theme(legend.position = "bottom")


fishsciences/fishpals documentation built on July 11, 2022, 12:32 a.m.