Make the most of R colors and palettes

There are a good deal of designed-in shades and all set-created palettes for R users

There are a good deal of designed-in shades and all set-created palettes for R users — if you know how to uncover and use them. In this article are some of my favourite guidelines and tools for doing the job with shades in R.

How to uncover designed-in R shades

There are more than 650 shades designed proper into base R. These permit you use color names as a substitute of hex or RGB codes. The color() function lists all of the color names, but that does not aid you see them.

There are web-sites and PDFs exactly where you can check out all the shades and what they appear like. But why not use your very own searchable table in R?

I designed a package deal to do just that, which you are welcome to download from GitHub working with install_github() from the remote controls or devtools packages:

remote controls::install_github("smach/rcolorutils", establish_vignettes = Accurate)

(Adding establish_vignettes = Accurate as an argument to install_github() installs the package deal vignette, much too.)

Load the package deal as common and then operate create_color_table() to display a sortable, search table of shades in R:

create_color_table(web site_duration = 10)

The create_color_table() function has a single optional argument, web site_duration, which defaults to twenty five.

Table of built-in R colors with names, color swatch, hex codes, and rgb values. Sharon Machlis, IDG

The create_color_table() function generates a table of designed-in R shades.

Whilst you can search by color names such as “blue,” not all blue-ish shades have “blue” in their names. That is why I included columns for RGB red, inexperienced, and blue values, so you can sort and filter by those people as perfectly. At minimum your shades may possibly end up in a more rational purchase than alphabetically by their names. To sort on more than a single column at a time, hold down the shift key when clicking column names.

Table of built-in R colors with names, color swatch, hex codes, and rgb values, searching for gr.y. Sharon Machlis, IDG

Hunting for gr.y in the rcolorutils package’s color table.

The table will allow you to search with normal expressions. For instance, you can search for gray or grey by working with a dot for “any letter” and hunting for gr.y in the table. If you do that, you will see that some shades are recurring with grey and grey in their names. So, though there are 657 color entries in R’s designed-in shades, there aren’t in fact 657 unique shades. 

How to search for ‘R shades like this one’

There is also a way to search for “colors fairly like this certain color” devoid of a table. I discovered this when working the base R color demo, which you can operate domestically with demo(shades).

The demo 1st reveals some displays of designed-in shades. I didn’t uncover those people very practical since the coloured textual content was not much too useful for comparing shades.  

List of built-in R colors, text of color names using the actual color Sharon Machlis, IDG

Screen of designed-in shades when working with demo(shades).

But if you cycle by means of those people coloured textual content displays, you will arrive at an alternative that states

## Now, contemplate deciding on a color by wanting in the
## community of a single you know :
plotCol(nearRcolor("deepskyblue", "rgb", dist=50))

and a display such as down below. That is practical!

Image of 5 different color blues: deepskyblue, deepskyblue2, turquoise2, darkturquoise, and cyan2. Sharon Machlis, IDG

Blues very similar to “deepskyblue.”

You could argue about just how blue these shades are when compared with other options, but it is a start. Observe, much too, that some have names like “cyan” and “turquoise,” which signifies you can not uncover those people in the table only by wanting for “blue.”

If you take a look at the code that created the higher than graphic of 5 blue shades, you will see that there ended up two capabilities associated: nearRcolor() and plotCol(). I wasn’t able to entry either of those people capabilities in base R devoid of working the shades demo. Considering that I’d like those people capabilities devoid of possessing to operate the demo each and every time, I extra code for each of them to my new rcolorsutils package deal.

If you operate nearRcolor() on an R color name, you get a named vector with color information. You can then plot those people shades with plotCol() — such as environment the variety of rows to display so all the shades never appear in a one row.

      .0000       .0281       .0374       .0403       .0589       .0643 
    "tomato"    "sienna1"     "brown1"      "coral"     "coral1"       "tan1" 
      .0667       .0723       .0776       .0882       .0918       .0937 
   "tomato2"    "sienna2"     "brown2"     "coral2"       "tan2" "firebrick1" 
plotCol(nearRcolor("tomato"), nrow = 3)
3 x 4 grid of colors like 'tomato' Sharon Machlis, IDG

Plot with a few rows of shades near to “tomato.”

If I appear for shades around “blue” I never get much too numerous returned:

.0000 .0667
"blue" "blue2"

I can modify how numerous effects I get back again by environment a custom rgb distance. What distance is ideal to use? I just fiddle close to with the distance integer till I get around the variety of shades I’d like to see. For instance, working with %>% pipe syntax and a distance of 135:

nearRcolor("blue", "rgb", dist = 135)  %>%
plotCol(nrow = 3)
3 x 3 grid of different shades of blue Sharon Machlis, IDG

Shades around blue with a customized distance.

The scales package deal also has a good function for plotting shades, exhibit_col(), which you can use as a substitute of plotCol():

nearRcolor("blue", "rgb", dist = 135) %>% 
3 x 3 grid of shades of blue, with some labels white but one label black. Sharon Machlis, IDG

The exhibit_col() function’s effects for shades around “blue.”

What’s good about exhibit_col() is that it decides no matter whether textual content color would appear improved as black or white, relying on the color becoming exhibited. 

How to uncover and use pre-created R color palettes

There are a couple color palettes designed into base R, but possibly the most well-liked occur from the RColorBrewer and viridis packages. You can install each from CRAN.

If you also install the tmaptools package deal, you will get a excellent designed-in application for discovering each RColorBrewer and viridis palettes by running palette_explorer().

The application lets you pick the variety of shades you want, and you can see all available palettes inside of that variety. The application contains sample code for producing the palettes, as you can see down below each individual palette color group. And it even has a color blindness simulator at the base proper.

Shiny app showing palettes from RColorBrewer and viridis Sharon Machlis, IDG

The tmaptools package’s palette_explorer application.

These could be all the palettes you will ever need to have. But if you are wanting for more assortment, there are other R packages with pre-created palettes. There are palette packages impressed by Harry Potter, Match of Thrones, Islamic art, U.S. national parks, and lots more. It can be tough to maintain observe of all of the available R palette packages — so the  paletteer package deal attempts to do that for us. Paletteer contains more than 2,000 palettes from 59 packages and classifies them into a few groups: discreet, steady, and dynamic.

I uncover it a little bit hard to scan and compare that numerous palettes. So, I created a Shiny application to see them by class. 

You can download the code for this application if you’d like to operate it on your very own technique:


Adjust the file extension from .txt to .R, install needed packages, and operate the application.R file in RStudio. Sharon Machlis


Screen shot showing Run App green triangle button within RStudio Sharon Machlis

Operate a Shiny application in RStudio by clicking the Operate Application inexperienced triangle

Adjust the file name from application.txt to application.R, make guaranteed you’ve set up the  needed packages, and then operate the application in RStudio with the “run app” button.

The application lets you search for palettes by class: steady, discreet, or dynamic. Then select the style you want, i.e., shades that diverge, shades that are in sequence, or shades that are qualitative devoid of any sort of purchase. These palette classifications occur from the paletteer package deal and a couple of them may possibly not be actual, so I tend to appear at all a few forms to make guaranteed I’m not lacking nearly anything I may possibly like.

Screen shot showing a color palette with R code underneath explaining how to use the palette. Sharon Machlis, IDG

Shiny application for displaying palettes included in the paletteer package deal.

Below each individual color graphic is code for how to use the palette. The 1st line of code reveals how to entry the vector of hex codes in the palette the 2nd a single reveals how to use it in ggplot with scale_fill_paletteer() or scale_color_paletteer() geoms. You can see how this operates in the video clip embedded at the top of this post.

Make your very own R palette and palette function

Often you will want to make your very own color palette, either for the reason that you’ve blended your very own shades in a plan you like or for the reason that you need to have to match your organization’s approved shades.

You can use any color hex codes inside of ggplot2::scale_fill_handbook(). Nevertheless, it is considerably more stylish to create my very own scale_fill() function very similar to ggplot2’s designed-in ones. The paletti package deal would make it very simple to do this.

Here’s how it operates. Initially operate the get_pal() function on your vector of shades to create a palette from them. Then operate either get_scale_fill() or get_scale_color() on the effects to flip the palette into a ggplots function, such as

my_shades <- c("#b7352d", "#2a6b8f", "#0f4461", "#26aef8")
scale_fill_my_palette <- get_pal(my_colors) %>%
# OR
col_fill_my_palette <- get_pal(my_colors) %>%

Now I can use my new scale_fill_my_palette() or col_fill_my_palette() function in a ggplot, as you can see with this plot of some toy info:

toy_info <- data.frame(
Group=c("A","B","C","A", "C") ,
xval=element(c("Mon", "Tue", "Wed", "Thur", "Fri"), degrees = c("Mon", "Tue", "Wed", "Thur", "Fri"), purchased = Accurate) ,
yval=c(255,500,459,342, 386)
ggplot(toy_info, aes(x = xval, y = yval, fill = Group)) +
geom_col() +
xlab("") +
ylab("") +
concept_vintage() +
Bar graph with dark red, dark blue, and light blue bars from a custom palette Sharon Machlis, IDG

Plot with customized palette.

Want more R guidelines and tutorials? Head to my Do Additional With R page.

Copyright © 2021 IDG Communications, Inc.