Get startedGet started for free

Customizing your layout

1. Customizing your layout

You've now seen how to customize your traces, how to use color to thoughtfully represent other variables, and how to customize the hover info. Now, you'll learn how to customize key features of your plot's layout.

2. layout()

The layout() function is the workhorse for polishing many elements of your plotly charts, including the axes, legend, plotting canvas, and size. If you're a ggplot2 user, then you can think about the layout() function as a combination of the labs() and theme() functions.

3. Axis labels

To begin, let's consider this scatterplot of total SO2 against free SO2. Notice that the default axis labels lack units, and are not as nicely formatted as is necessary for publication.

4. Axis labels

To change the axis labels we add the xaxis and yaxis arguments to layout(). These arguments require lists, so to change the x-axis label, we pass a list with the title element equal to the string "Free SO2 (ppm)" and to change the y-axis label we pass a list with the title element equal to the string "Total SO2 (ppm)".

5. Titles

Now that we have a plot with clearly labeled axes, we want to add an informative title to capture our reader's attention. To do this we pass a character string to the title argument of layout, just like using labs() in ggplot2. Why do we pass lists to the xaxis and yaxis and not title? Because we can customize numerous elements of the axes, and a list allows you to specify multiple customizations at once.

6. Transforming axes

For example, we can format the axes, such as by applying a natural log transformation. In this example, we apply a natural log transformation to both axes by adding type equals "log" as an element to the lists passed to xaxis and yaxis. Notice that the axis labels are nicely formatted to reflect the log transformation on the original scale. Other possible axis types include linear, date, and category.

7. Customizing the grid

Let's return to the untransformed scatterplot of free and total sulfur dioxide. Looking past the points on the plot, notice that plotly adds black lines to represent x equals 0 and y equals 0, and adds a faint gray grid in the background. The xaxis and yaxis arguments of layout() also let us customize these aspects.

8. Customizing the grid

For example, if you wish to remove the zero lines, you can specify zeroline equals FALSE in the xaxis and yaxis lists. Similarly, if you wish to remove the grid lines, you can specify showgrid equals FALSE. Here, we've combined these two ideas, adding showgrid equals FALSE to the yaxis list to remove the horizontal grid lines, and adding zeroline equals FALSE to both the xaxis and yaxis lists to remove the lines at x and y equals 0.

9. Customizing the canvas

Now that your axes and grids are customized, let's turn to other aspects of the plotting canvas. The paper underscore bgcolor and plot underscore bgcolor arguments allow you to alter the color of the exterior and interior of the plot, respectively. For example, to change the interior of the plot to gray90, you add plot underscore bgcolor equals toRGB("gray90"). The toRGB() function allows you to pass R colors easily into plotly. You can also use other specifications such as hex codes directly. If you want to change the margins to sky blue, then you also need to add paper underscore bgcolor equals toRGB("skyblue").

10. Let's practice!

We've only scratched the surface of the customizations possible using the layout function, so be sure to experiment as you practice!