This page aims to provide useful R code to custom the background of your ggplot2 plots. More generally, the R graph gallery proposes 3 pages dedicated to the layout customisation: one concerning axis, one concerning legends, and this one concerning background.

Here we want to modify non-data components, and it is often done through the theme() command. This page is strongly inspired from the help page of ggplot2 (?theme). Also, do not hesitate to visit the very strong ggplot2 documentation for more information.

We start with this really basic plot relying on the mtcars dataset with a default background:






You can easily color the background. There are 2 main areas: plot background (here in green) and panel background (here in red)



Then you can customise the grid. There are 2 types of grid: major and minor ones. To customise them, you can use the usual arguments of the element_line function. Note that you can apply the modifications to the X axis or the Y axis separately.

Not what you are looking for ? Make a new search !

Leave a Reply

Notify of