How to save & load a custom color palette in an R package

I'd like to start including custom color labels in my R package

my_orange <- '#ff6901'
my_purple <- '#6f54a3'
my_green <- '#53b647'
my_dark_green <- '#118482'

What is a practical place and file format to save these colors so that they are loaded into the environment when my package is loaded?

1 Like

I'm not sure if this is the "right" way, but one way is to include a vector of colors in your package. For example, I created a package with a bunch of utility functions and vectors that I use in my work. One file in the package is called utility_vectors.r where I keep various objects, one of which is a vector of my school's colors. An analogous version for your colors would be:

#' Vector of custom colors
#'
#' @examples
#' mycolors["my_orange"]
#'
#' @export
mycolors = c(
  my_orange = '#ff6901',
  my_purple = '#6f54a3',
  my_green = '#53b647',
  my_dark_green = '#118482'
)
3 Likes

@joels’s answer seems like a good one. If you want to go a bit further and create custom palettes and ggplot2 scales, I’ve found this blog post to be a great resource.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.