Change Colors of plotly Graph in R (Example)

 

This tutorial provides several examples to customize the colors in plotly graphs in the R programming language.

Compared to other graphing libraries, plotly excels at its color customization. However, this level of power comes with added complexity.

To be fully in control of your graphs (from the legend to the background paper to the marker outlines to the tick marks and beyond!) you should become familiar with the plotly reference library and expect to spend lots of time doing trial-and-error experiments.

The table of contents of this tutorial is shown below:

 

 

Kirby White Researcher Statistician Programmer

Note: This article was created in collaboration with Kirby White. Kirby is an organizational effectiveness consultant and researcher, who is currently pursuing a Ph.D. at the Seattle Pacific University. You can read more about Kirby here!

 

Let’s begin!

 

Packages and Example Data

If you have not already done so, install and load these packages:

  • plotly
  • tidyverse

We’ll use the midwest dataset for this example, which is included with the plotly package.

df <- as.data.frame(ggplot2::midwest)

 

Color and Data Types

The best colors to encode in your graphs depend on the type of data you’re representing.

There are three types of color palettes we might want to use:

  • Sequential palettes are best used on data that has a clear order from low to high, such as “Small”, “Medium”, and “Large” or numeric data going from 0 – 100
  • Qualitative palettes are best used on categorical data with no inherent order or magnitude, such as “United States” or “Iceland” or “Germany”
  • Diverging palettes are best used when you want to emphasize the mid-point as well as the high and low end. For instance, you might want to display how far above or below is data point is from the average value

Most graphing libraries in R use the “Color Brewer” palettes, which are available in the RColorBrewer package (and is automatically installed alongside plotly or ggplot2).

To quickly see the names and color scales within each type of palette, run this from your R console:

RColorBrewer::display.brewer.all()

Now that you have a basic idea of the available colors, let’s use them in our plotly graphs.

 

Qualitative Palettes

The pre-built qualitative palettes and the maximum number of colors they can display:

  • Accent (8)
  • Dark2 (8)
  • Paired (12)
  • Pastel1 (9)
  • Pastel2 (8)
  • Set1 (9)
  • Set2 (8)
  • Set3 (12)

Let’s create a scatterplot (or xy-plot) to look at the relationship between the rates of high school graduation and college education for each county. We’ll encode the state in the color of each dot by setting color = ~state and we’ll pick a qualitative color palette with the colors argument.

plot_ly(
  data = df,
  x = ~perchsd,
  y = ~percollege,
  type = "scatter",
  mode = "markers",
  color = ~state,
  colors = "Set1"
  )


You can also pass a vector of color names or hex codes to match specific colors to a given category. Here’s an example to demonstrate both methods at once:

plot_ly(
  data = df,
  x = ~perchsd,
  y = ~percollege,
  type = "scatter",
  mode = "markers",
  color = ~state,
  colors = c("red", "blue", "black", "#32a852", "#c10dd1")
  )


 

Sequential Palettes

Sequential palettes use different shades of one or two colors to create a smooth gradient to represent changes in magnitude. This is best for ordered data, such as a range of numeric measurements or a scale of customer responses from “Very Dissatisfied” to “Very Satisfied”.

The pre-built sequential palettes and the number of colors they contain are:

  • Blues
  • Greens
  • Greys
  • Oranges
  • Purples
  • Reds
  • YlOrRd (yellow < red)
  • YlOrBr (yellow < brown)
  • YlGnBu (yellow < green < blue)
  • YlGn (yellow < green)
  • RdPu (red < purple)
  • PuRd (purple < red)
  • PuBuGn (purple < blue < green)
  • PuBu (purple < blue)
  • OrRd (orange < red)
  • GnBu (green < blue)
  • BuPu (blue < purple)
  • BuGn (blue < green)

For this example, we’ll calculate the total population in each state and display it in a bar chart. The color of each bar corresponds to its numeric value, such that those with smaller populations are more yellow and those with larger populations are more red.

df %>%
  group_by(state) %>% 
  summarize(tot = sum(poptotal)) %>% 
  plot_ly(
    x = ~state,
    y = ~tot,
    type = "bar",
    color = ~tot,
    colors = "YlOrRd"
  )


To specify the color to use as the low and high values, you can specify it with a common color name or hex code, like this:

df %>%
  group_by(state) %>% 
  summarize(tot = sum(poptotal)) %>% 
  plot_ly(
    x = ~state,
    y = ~tot,
    type = "bar",
    color = ~tot,
    colors = c("#1B98E0","black")
  )


This is a great opportunity to bring custom brand colors into your graphics! You might have noticed that these colors are commonly used on the Statistics Globe website.

 

Diverging Palettes

Just like sequential palettes, diverging palettes use different shades of one or two colors to create a smooth gradient to represent changes in magnitude. However, the position of those colors is altered so that the shade changes to represent a value’s distance from the midpoint instead of from the lowest value.

The pre-built sequential palettes and the number of colors they contain are:

  • Spectral (low:red, mid:yellow, high:purple)
  • RdYlGn (low:red, mid:yellow, high:green)
  • RdYlBu (low:red, mid:yellow, high:blue)
  • RdGy (low:red, mid:white, high:grey)
  • RdBu (low:red, mid:white, high:blue)
  • PuOr (low:purple, mid:white, high:organge)
  • PRGn (low:purple, mid:white, high:green)
  • PiYG (low:pink, mid:white, high:green)
  • BrBG (low:brown, mid:white, high:green)

For this example, we’ll calculate the difference between each county’s poverty level and the poverty for the entire midwest. We can display it with a scatter plot so that we can see each county as a dot, but seperate the dots by the state they belong to. We’ll set colors = "PuOr" to specify a diverging color scale:

df %>% 
  plot_ly(
    x = ~state,
    y = ~percbelowpoverty,
    type = "scatter",
    color = ~percbelowpoverty,
    colors = "PuOr"
  ) %>% 
  layout(plot_bgcolor = "#bababa")# increases contrast between the dots and background


Keep in mind that larger values indicate more poverty, so we might want to use a color generally perceived as something negative (like red or black). We can specify custom colors for our diverging scale with a vector of three colors in order of low, mid, high.

df %>% 
  plot_ly(
    x = ~state,
    y = ~percbelowpoverty,
    type = "scatter",
    color = ~percbelowpoverty,
    colors = c("#4e9665","white","#c26d11")
  ) %>% 
  layout(plot_bgcolor = "#bababa")# increases contrast between the dots and background


 

Further Resources

 
You may check out the articles below for more detailed examples and videos of popular charts in plotly:

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

  • When using color = ~name, the name of the legend of gradient scale is the same as used variable name.
    How to change it to user specified name?

    Reply
    • Hello Mark,

      You can use the layout function for that. Here is the R code example.

      plot_ly(
        data = df,
        x = ~perchsd,
        y = ~percollege,
        type = "scatter",
        mode = "markers",
        color = ~state,
        colors = "Set1")%&gt;% layout(
          legend = list(title = list(text = 'Your Legend Title')))

      Best,
      Cansu

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top