Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cararthompson/monochromeR

A package for creating monochrome colour palettes and easily converting rgba values to hex codes
https://github.com/cararthompson/monochromeR

color-palette dataviz ggplot2 palette palette-generation rstats

Last synced: 3 days ago
JSON representation

A package for creating monochrome colour palettes and easily converting rgba values to hex codes

Awesome Lists containing this project

README

        

---
title: "monochromeR: an easy way to create monochrome colour palettes"
output: github_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(magrittr)
```

![](inst/figures/logo-social.PNG)

## What does the package do?

This package generates a monochrome palette from a starting colour for a specified number of colours. Users can decide whether to go darker, lighter, or both ways from that starting colour, which can be provided as a vector of rgb values (e.g. `c(15, 75, 99)`), a hex colour code (e.g. `#0F4B63`) or a recognised colour name (e.g. `"purple"`). The package can also display the generated palette in the plot window, with or without hex colour code labels.

## How can I use it?

This package is available on [CRAN](https://cran.r-project.org/), so can be installed using `install.packages("monochromeR")`.

Alternatively, to install it from here, use `remotes::install_github("cararthompson/monochromeR")`. (To do this, you need to have installed the `remotes` package. To do that, use `install.packages("remotes")`.)

## Can we see some examples?

Sure! Here goes. To make the examples easy to read, I will use recognised colour names rather than hex codes or rgb values.

### Generate monochrome palettes with `generate_palette()`

```{r, fig.height=2}
library(monochromeR)

generate_palette("purple", modification = "go_lighter",
n_colours = 5, view_palette = TRUE)

```

The functions allow for British spelling and US spelling of colour/color.

```{r, fig.height=2}
generate_palette("purple", modification = "go_darker",
n_colors = 5, view_palette = TRUE, view_labels = FALSE)

```

With more colours, the hex codes get harder to view in the plot. They are printed in the console when the function is called on its own, and can also be assigned to an object for later use.

```{r, fig.height=2}
purple_palette <- generate_palette("purple", modification = "go_both_ways",
n_colours = 20, view_palette = TRUE, view_labels = FALSE)

purple_palette

```

And just because it was easy to implement, this function can also be used to blend two colours together:

```{r, message=F, warning=F, fig.height=2}
generate_palette("purple", blend_colour = "green",
n_colours = 10, view_palette = TRUE, view_labels = FALSE)

```

### Get the hex colour code from an rgb or rgba vector

```{r}
# Get hex code from rgb
rgb_to_hex(c(15, 75, 99))

# Get hex code from rgba
rgba_to_hex(c(15, 75, 99, 0.8))

```

### Get the rgb values from a hex code

```{r}
# Get the rgb values from the hex code
hex_to_rgb("#FFFFFF")

# Get the rgb values from the hex code
hex_to_rgb("#0F4B63")

```

#### View any palette, with or without labels

```{r, fig.height=2}
view_palette(c("red", "yellow", "purple", "green"), view_labels = FALSE)

view_palette(c(wesanderson::wes_palettes$Moonrise1,
wesanderson::wes_palettes$Moonrise2[1:2]))

```

From version 0.2.0 onwards, if you have created a named vector for your colours (which I highly recommend!), the names you have provided are displayed alongside the hex codes.

```{r, fig.height=2}
banana_palette <- c("unripe" = "#89973d",
"ripe" = "#e8b92f",
"overripe" = "#a45e41")

view_palette(banana_palette)

```

## Easily pass the output to functions which check accessibility

Version 0.1.3 onwards exports a ggplot object, which can be passed to functions such as colorblindr::cvd_grid() to check how the palette is perceived by people with different visual perception. With `view_labels = TRUE`, the labels are displayed in black and white on top of the colour, to allow users to easily see how readable the text is.

```{r, fig.height=2}
view_palette(c("red", "yellow", "purple", "green"), view_labels = TRUE)
```

```{r, fig.height=4}
colorblindr::cvd_grid()
```

## Worked examples: using `monochromeR` within datavisualisations

### Using `generate_palette` within `scale_colour_manual()`

Here's a simple example, using `{monochromeR}`'s `generate_palette()` to create a colour palette on the fly within `ggplot()`.

```{r, eval=T, echo=T, warning=F, message=FALSE}
library(tidyverse)
library(monochromeR)

penguin_plot <- palmerpenguins::penguins %>%
ggplot() +
geom_point(aes(x = flipper_length_mm, y = bill_length_mm,
colour = species, size = body_mass_g),
alpha = 0.8) +
labs(title = "Perfectly proportional penguins",
subtitle = "Each dot represents a penguin. The bigger the dot, the heavier the penguin. \nLook at them go!",
x = "Flipper length (mm)",
y = "Bill length (mm)") +
scale_size(guide = "none") +
guides(colour = guide_legend(title = "")) +
theme_minimal() +
theme(plot.subtitle = element_text(margin = margin(6, 0, 12, 0)))

penguin_plot

penguin_plot <- penguin_plot +
scale_colour_manual(values = generate_palette(c(15, 75, 99),
modification = "go_both_ways",
n_colours = 3))

penguin_plot

```

### Creating a unified aesthetic across all aspects of the dataviz

Here's an example using `{monochromeR}`'s `generate_palette()` to generate all the colours used in the plot, resulting in a more polished look with minimal effort.

```{r, eval=T, echo=T, warning=F, message=FALSE, fig.height=2}
penguin_palette <- generate_palette(c(15, 75, 99),
modification = "go_both_ways",
n_colours = 8,
view_palette = T,
view_labels = F)
```

```{r, eval=T, echo=T, warning=F, message=FALSE}

penguin_plot +
scale_colour_manual(values = penguin_palette[c(1, 3, 5)]) +
theme_minimal() +
theme(plot.background = element_rect(fill = penguin_palette[8],
colour = penguin_palette[8]),
panel.grid = element_line(colour = penguin_palette[7]),
panel.background = element_rect(fill = penguin_palette[8],
colour = penguin_palette[8]),
text = element_text(colour = penguin_palette[3]),
axis.text = element_text(colour = penguin_palette[3]),
plot.title = element_text(colour = penguin_palette[1], hjust = 0, size = 16),
plot.subtitle = element_text(colour = penguin_palette[2], hjust = 0))
```

## Extra resources

### Here are some resources I found helpful in making this package

- **For the mechanics of how to make a package**: [Your first R package in 1 hour](https://www.pipinghotdata.com/posts/2020-10-25-your-first-r-package-in-1-hour/), a tutorial by Shannon Pileggi
- **For the colour conversions**: [This thread on StackOverflow](https://stackoverflow.com/questions/60977641/r-function-for-rgba-to-hex-colour-conversion)
- **For the logo**: [The hexSticker package](https://github.com/GuangchuangYu/hexSticker) by Guangchuang Yu

### Bugs and queries

I've done my best to make the functions in this package user-friendly, and to make the error messages easy to understand. If you come across a bug or an error message that doesn't make sense, or if there's something you think would make this package better, [please let me know](https://github.com/cararthompson/monochromeR/issues)!