-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
126 lines (101 loc) · 3.92 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# MPAcolors
<!-- badges: start -->
<!-- badges: end -->
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](https://github.com/) with:
``` r
# 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.
```{r setup}
library(MPAcolors)
library(ggplot2)
library(dplyr)
```
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
```{r}
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.
```{r fill}
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.
```{r custom-colors}
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.
```{r continuous}
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
```{r complementary}
scales::show_col(mpa_palette("complementary"), cex_label = 2)
```
### Highlight palette
```{r highlight}
scales::show_col(mpa_palette("highlight"), cex_label = 2)
```