Get startedGet started for free

Customize your traces

1. Customize your traces

You've seen how to create a variety of common graphics using plotly. In this chapter, you'll learn how to style and customize your graphics so that they are ready to publish. To begin, you'll learn how to style traces.

2. Wine quality data

We'll revisit plots we created in the last chapter based on the wine quality dataset. Recall that the dataset contains the results of a chemical analysis and assessment of a sample of 325 wines.

3. Color

First, let's consider how to change the color of the markers placed on the canvas when you add a trace. As an example, let's create a histogram of fixed acidity. By default, the bars are blue, but what if you don't like blue, or if you want to match the theme of your website or organization?

4. Color

To change the color of all markers placed in a trace, such as all of the bars in this histogram, we add the color argument to the trace. For example, to create a red histogram we add the argument color equals I("red"). Here we use the as is function, I(), to set the color of the histogram. Without this function, plotly assumes that you are mapping a variable to the color. We'll talk about that in the next lesson.

5. Opacity

Another way to customize your graphics is to adjust the opacity of the markers. For example, on this scatterplot of fixed_acidity against residual_sugar many points are overlapping or close together, making the data appear as a blob on the left side. This is an issue called overplotting. One approach to overcome overplotting is to increase the transparency of the points, or said another way, to decrease the opacity of the points.

6. Opacity

Here is the scatterplot where points are only 20% opaque; that is, they are 80% transparent. This effect allows us to see the density of points on the scatterplot: high-density regions appear darker than low-density regions. Only one change is made to the code: we pass a list with opacity equals 0-point-2 to the marker argument to add underscore markers.

7. Symbols

Another way to address the issue of overplotting is to change the plotting symbol from a filled glyph to an open glyph. For example, an open circle is drawn for each point on this version of the scatterplot. To change the plotting symbol to an open circle, we pass a list with element symbol equals "circle-open" to the marker argument of the trace. Notice that we certainly get more of a sense of point density than the original version, but adjusting the opacity is probably preferred.

8. Marker options

There are other marker options that we haven't discussed here, but they are intuitive to add. For example, we can change the size of the points on our scatterplot by adding a size argument to the list. Or we can specify the width of the lines dividing bars on histograms and bar charts.

9. Let's practice!

Now that you know the basics, it's time to practice customizing charts.