# knitr::opts_chunk$set( # collapse = TRUE, # comment = "#>" # )
library(IFRthemes) library(ggplot2) library(dplyr)
All IFR plots should be produced in ggplot2 using a standard theme and consistent colour palette. The IFRthemes
package allows the user to apply a custom IFR theme and two different colour palettes depending on the number of colours required.
You can install the package from GitHub using the remotes::install_github
function. Using build_vignettes = TRUE allows you to access this vignette.
remotes::install_github("InStreamFisheries/IFRthemes", build_vignettes = TRUE, force = TRUE) library(IFRthemes)
Once the package is installed, you can access this vignette via the following command:
browseVignettes("IFRthemes")
To use the IFR theme, simply add + theme_ifr() to a ggplot object.
ggplot(mtcars, aes(mpg, disp)) + geom_point(size = 3) + theme_ifr()
The theme also formats panels.
ggplot(mtcars, aes(mpg, disp)) + geom_point(size = 3) + facet_wrap(~cyl) + theme_ifr()
There is one argument to the theme: base_size, which scales the size of plot text. The function defaults to 16, which sizes nicely for 6 inch figures. In some cases (e.g., very large plots with lots of faceting) you may need to change the text size. The example below shows how to apply this argument.
ggplot(mtcars, aes(mpg, disp)) + geom_point(size = 3) + facet_wrap(~cyl) + theme_ifr(base_size = 10)
It's easy to adjust the theme. Below the theme is overwritten to angle the x-axis text.
ggplot(mtcars, aes(mpg, disp)) + geom_point(size = 3) + theme_ifr() + theme(legend.position = c(1, 1), axis.text.x = element_text(angle = 45, hjust = 1))
We have created two colour pallettes for use within ggplot2. The first colour palette (scale_colour_ifr
or scale_fill_ifr
) consists of six colours that are complementary to IFR's logo, which should be used for all plots with six or less colours. The second palette (scale_colour_ifr10
or scale_fill_ifr10
) is a larger palette of 10 colourblind-friendly colours that is used whenever there are more than six colours in a plot (typically rare).
The custom palettes are applied like any other ggplot colour palette.
ggplot(mtcars, aes(as.factor(carb), fill = as.factor(carb))) + geom_bar() + scale_fill_ifr() + theme_ifr()
ChickWeight %>% mutate(Chick = as.factor(Chick)) %>% group_by(Chick) %>% summarise(Mean = mean(weight), .groups = 'drop') %>% slice(1:10) %>% ggplot(., aes(x = Chick, y = Mean, fill = Chick)) + geom_col() + scale_fill_ifr10() + theme_ifr()
A simple function (show_colour_ifr
) was created to display the six custom colours and allow the user to select colours for a custom plot. For example, when overlaying multiple plot types or adding lines to plots, the user may want to select colours without applying the full palette.
show_ifr_colours()
ggplot(mtcars, aes(mpg, disp)) + geom_point(size = 3) + theme_ifr() + geom_vline(xintercept = 20, size = 3, color = "#a40c01") + geom_hline(yintercept = 300, size = 3, color = "#104692")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.