Get startedGet started for free

Thoughtful use of color

1. Thoughtful use of color

In the last lesson, you learned how to style your traces. In this lesson, you'll learn how to use color to thoughtfully represent the values of a variable in your dataset.

2. A nonlinear relationship?

To begin, consider this scatterplot of alcohol against flavonoids in wine. A scatterplot with a smoother reveals a possibly nonlinear relationship between the variables. However, the plot doesn't consider how this relationship might be explained by a third variable, such as the wine type.

3. Adding a third variable

Once we add color representing wine type to this scatterplot, the nonlinear relationship is explained. The overall trend appears nonlinear since the relationship between alcohol and flavanoids differs by wine type. As always, we begin by piping in the data and specifying our aesthetic mappings. To add color to the plot we specified that Type should be mapped to color, in addition to mapping Flavanoids and Alcohol to the x- and y-axes. Then, we add the markers trace to create the scatterplot.

4. Adding a quantitative variable

In the previous example, we used color to represent a factor, but color can also be used to add a third numeric variable to a scatterplot. For example, here we use color to represent the color intensity of the wine. It appears that wines with higher alcohol content tend to have higher color intensities. Notice that plotly is using a color gradient to represent this numeric variable. We again only need to add an aesthetic mapping for color and plotly determines the method to display the variable as well as the default color palette. This is important to keep in mind in case factors have been recorded and loaded as numbers. In this situation, you may wish to convert the variable to a factor so that discrete colors are chosen.

5. RColorBrewer palettes

The default color palettes chosen by plotly are often sensible, but there are times that you may wish or need to change them. For example, you may wish to make your graphic more colorblind safe or match the theme of the website where you plan to host the graphic. Let's change the palette of this scatterplot coded by wine type. To change the colors used in the scatterplot, we add the colors argument to the markers trace. There are numerous palettes available in plotly, such as those provided in the RColorBrewer package. Here, we specify colors equal "Dark2" to change the palette.

6. Manual palettes

If you are trying to style your graph according to a specific theme, it's unlikely that the palette is already defined in the RColorBrewer package. Luckily, the colors argument accepts vectors of valid R color code. For example, we can pass colors the character vector "orange", "black", "skyblue" to change the palette. In addition to named colors, you can also use numerous formats including rgb and hex to define your palette. At this point, it's important to note the difference in syntax between changing a palette and setting the value of all points using the as is function. When you change a palette, you have already mapped a variable, such as wine type, to the color aesthetic; thus, the color argument in add_markers() knows that the vector of colors is referring to the palette and not the individual observations.

7. Let's practice!

Let's practice using color to represent variables.