Skip to content

Latest commit

 

History

History
146 lines (117 loc) · 4.28 KB

README.md

File metadata and controls

146 lines (117 loc) · 4.28 KB

MPAcolors

The goal of MPAcolors is to provide colors used by the Swedish Medical Products Agency (MPA) in their publications and reports.

The package also provides functions to generate color palettes for use in ggplot2 plots.

Installation

You can install the development version of MPAcolors from GitHub with:

# install.packages("devtools")
devtools::install_github("vrognas/MPAcolors")

Using these colors in plots

Let’s look at some examples to see the various ways these functions can be used to customize colors with ggplot2. Below is a very simple bar chart using the palmerpenguins::penguins data set.

library(MPAcolors)
library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

The new mpa_color function can be used to specifically define a color. In the plot below, mpa_color(“mpa_blue”) is the fill argument in the geom_bar layer to make all of the bars that specific shade of mpa_blue

palmerpenguins::penguins |>
  count(species) |> 
  ggplot(aes(x = species, y = n)) +
  geom_bar(stat = "identity", fill = mpa_color("mpa_blue")) +
  labs(title = "The count of each species in the palmerpenguins data set") +
  scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
  theme_linedraw() +
  theme(axis.ticks = element_blank(),
        axis.title = element_blank(),
        panel.grid.major.x = element_blank())

We could also add fill = species to the aes layer (meaning that each species will have its own color) and then use scale_fill_mpa(palette = “complementary”) to automatically apply our “complementary” color palette.

palmerpenguins::penguins |>
  count(species) |> 
  ggplot(aes(x = species, y = n, fill = species)) +
  geom_bar(stat = "identity") +
  scale_fill_mpa(palette = "complementary") +
  labs(title = "The count of each species in the palmerpenguins data set") +
  scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
  theme_linedraw() +
  theme(axis.ticks = element_blank(),
        axis.title = element_blank(),
        legend.position = "none",
        panel.grid.major.x = element_blank())

Or if you want to use the custom colors but not a specific palette, add a scale_fill_manual layer and specify the values using the mpa_color function.

palmerpenguins::penguins |>
  count(species) |> 
  ggplot(aes(x = species, y = n, fill = species)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(values = 
                      unname(c(mpa_color("pink_complement_20","yellow_complement","mpa_blue_60")))) +
  labs(title = "The count of each species in the palmerpenguins data set") +
  scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
  theme_linedraw() +
  theme(axis.ticks = element_blank(),
        axis.title = element_blank(),
        legend.position = "none",
        panel.grid.major.x = element_blank())

Lastly, below is an example of a continuous color scale, with our scale_color_mpa_c function specifying the palette to be used.

palmerpenguins::penguins |>
  filter(!is.na(sex)) |> 
  ggplot(aes(x = sex, y = body_mass_g, color = bill_depth_mm)) +
  geom_jitter(size = 3, width = 0.3) +
  scale_color_mpa_c(palette = "highlight", direction = -1) +
  labs(
    title = "Bill depth and body mass by sex",
    y = "Body mass (g)",
    color = "Bill depth (mm)"
  ) +
  theme_linedraw() +
  theme(
    axis.ticks = element_blank(),
    axis.title.x = element_blank(),
    panel.grid.major.x = element_blank()
  )

Complementary palette

scales::show_col(mpa_palette("complementary"), cex_label = 2)

Highlight palette

scales::show_col(mpa_palette("highlight"), cex_label = 2)