Get startedGet started for free

Layering traces

1. Adding layers

In the previous chapters, you have focused on creating a plotly chart based on a single trace. In this lesson, you will learn how to layer traces to create more complex charts. Specifically, you'll learn how to add smoothers to scatterplots and overlay density plots. While complex charts often look impressive, it's important to remember that the simplest chart that conveys your message is usually the best choice. So layer cautiously.

2. Wine data

In this lesson, we'll return to the wine dataset considered in chapters 1 and 2. Recall that the dataset consists of 13 chemical measurements on three types of wine.

3. Adding a smoother

As a first example, consider adding a LOESS smoother to the scatterplot of alcohol content against flavonoids to highlight the nonlinear structure. We begin by fitting the LOESS model and storing it as the object m. Next, we construct a scatterplot with Flavonoids on the x-axis and Alcohol on the y-axis. To add another layer, we pipe the plot into another trace layer. Here, we wish to add a line for representing the fitted model, so we add the lines trace. To plot the fitted values we need to map Flavonoids to the x-axis and the fitted values to the y-axis. Notice here that we only need to explicitly map fitted(m) to the y aesthetic because the x aesthetic will be inherited from the base layer. Finally, we specify showlegend equals FALSE to remove the legend. If we retained the legend, then each trace would be represented, and it seems silly to have a legend differentiating the observed points from the fitted values.

4. Adding a second smoother

To compare two models we can add multiple layers to the chart. For example, we may wish to compare how a second-order polynomial fits the data compared to the LOESS fit. We begin by fitting the polynomial model using the lm() function and storing it as the object m2. The LOESS model is still stored in the object m. The code to create the chart is nearly identical to the previous slide, but we now have three layers with different traces: we add the points using add_markers(), the LOESS fit using the first add_lines() trace, and the polynomial fit using the second add_lines() trace. The key differences in the code relate to the creation of the legend, which is necessary to differentiate the two models. We can name each trace by adding the name argument. Here, we have added name equals "LOESS" and name equals "Polynomial". In the markers trace, we specify showlegend equals FALSE to remove this trace from the legend since it is obvious that the points represent observed data.

5. Layering densities

Layering is also a useful tool when comparing distributions. For example, here we layer density plots to compare the distribution of Flavanoids between wine types. If you're not familiar with density plots, you can think of them as smoothed histograms.

6. Layering densities

Layering density plots requires three steps: First, we create subsets for each wine type using filter(). Next, we use the density() command to calculate the x, y coordinates needed for the smoothers. We then add three layers to our chart using add_lines(), mapping x and y to the appropriate aesthetics, and setting the name of the trace to the wine type. We also polish the axis labels.

7. Let's practice!

In this lesson, you learned how to overlay multiple traces to create more complex charts. Now that you understand the concept, it's time to practice.