Giter VIP home page Giter VIP logo

monochromer's Introduction

monochromeR: An easy way to create monochrome colour palettes

CRAN_Status_Badge

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, 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()

library(monochromeR)

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

## [1] "#A020F0" "#B34CF3" "#C679F6" "#D9A5F9" "#ECD2FC"

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

generate_palette("purple", modification = "go_darker", 
                 n_colors = 5, view_palette = TRUE, view_labels = FALSE)

## [1] "#A020F0" "#8019C0" "#601390" "#3F0C5F" "#200630"

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.

purple_palette <- generate_palette("purple", modification = "go_both_ways", 
                                   n_colours = 20, view_palette = TRUE, view_labels = FALSE)

purple_palette
##  [1] "#ECD2FC" "#E4C0FA" "#DCAEF9" "#D59CF8" "#CD8BF7" "#C679F6" "#BE67F4"
##  [8] "#B655F3" "#AF43F2" "#A731F1" "#A020F0" "#931DDC" "#861AC9" "#7918B6"
## [15] "#6C15A3" "#601390" "#53107C" "#460E69" "#390B56" "#2C0843"

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

generate_palette("purple", blend_colour = "green", 
                 n_colours = 10, view_palette = TRUE, view_labels = FALSE)

##  [1] "#A020F0" "#9133DA" "#8347C5" "#755BB0" "#676F9A" "#588385" "#4A9670"
##  [8] "#3CAA5A" "#2EBE45" "#20D230"

Get the hex colour code from an rgb or rgba vector

# Get hex code from rgb
rgb_to_hex(c(15, 75, 99))
## [1] "#0F4B63"
# Get hex code from rgba
rgba_to_hex(c(15, 75, 99, 0.8))
## [1] "#3E6E82"

Get the rgb values from a hex code

# Get the rgb values from the hex code
hex_to_rgb("#FFFFFF")
## [1] "r = 255, g = 255, b = 255"
# Get the rgb values from the hex code
hex_to_rgb("#0F4B63")
## [1] "r = 15, g = 75, b = 99"

View any palette, with or without labels

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.

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.

view_palette(c("red", "yellow", "purple", "green"), view_labels = TRUE)

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().

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.

penguin_palette <- generate_palette(c(15, 75, 99), 
                                    modification = "go_both_ways", 
                                    n_colours = 8,
                                    view_palette = T,
                                    view_labels = F)

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

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!

monochromer's People

Contributors

cararthompson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

raphak

monochromer's Issues

hex_to_rgb() returns string not matrix

Hi!

Super cool package. I used it to extract hex codes for transparent values of a whole palette to use as different layers of a ggplot2, as the colours represent different levels of data (see below).

To do this I passed a colour palette in hex codes to hex_to_rgb() then used rgba_to_hex() with an alpha value to get the new colour palette. However hex_to_rgb() currently returns the string "r = X, g = Y, b = Z", when a vector of the three colours would be more useful maybe?

My code and function are here:

# set colours for plot
colours <- RColorBrewer::brewer.pal(10, 'Spectral')

# function to make points lighter but not transparent
alpha_hex <- function(hex_code, alpha = 1){
  # split by ',' and extract number
  temp <- monochromeR::hex_to_rgb(hex_code) %>%
    str_split(., ',') %>%
    .[[1]] %>%
    parse_number
  
  return(rgba_to_hex(c(temp, alpha)))
}

purrr::map_vec(colours, alpha_hex, alpha = 0.7)

pcoa_all_panel

Small Typo in "How can I use it?" Section

Hi Cara,
This a great package. Looking forward to learning more about it in your Workshop for Ukraine today. Just wanted to let you know that there was a small typo in the How can I use it? section.

Instead of remotes::github_install("cararthompson/monochromeR") it should be remotes::install_github("cararthompson/monochromeR").

Thanks,
Kurtis

Generate a ggplot object from view_palette() instead of a base R plot

view_palette() currently uses scales::show_col() to generate a visual representation of a chart, which uses plot() to generate base R plot.

It would be convenient if view_palette() generated a ggplot object so that it could be passed into colorblindr::cvd_grid() to check how a palette appears for folks with colour blindness.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.