In part 2 of this guide looking at supergrid() - a new function to easily add grids to base R plots with major and minor grid lines - this part shows you how to create and add themes to re-use the same styles for different plots. Read on to learn more!
Guide Information
Title | Supergrid - Creating and using themes with supergrid. |
Author | Benjamin Bell |
Published | July 31, 2025 |
Last updated | August 4, 2025 |
R version | 4.5.0 |
Packages | base |
Navigation |
This is a 2 part guide:
Part 1: Supergrid | How to use supergrid() and customise grids for base R plots |
Part 2: Supergrid themes | How to create and use themes with supergrid(), and use built-in themes. |
Themes
As seen in part 1 of this guide, you can customise the grid with many different styles and options.
You may wish to use the same style grid on multiple plots. Rather than repeating the same code multiple times, you can instead re-use the code to re-create the style using R's built in functions.
Creating and using themes
To create and use your own theme, you simply need to save the named arguments as a list object, then use the do.call()
function to use them. Let's consider the following example.
x <- 1:10
plot(x)
supergrid(d=2, col="red", lty=c(2, 1), lwd=c(2, 1))
This would result in a plot with a grid, using red dash line for the major lines, and solid red lines for the minor, with only 2 divisions.
In this example, the "named arguments" refer to d
, col
, lty
and lwd
.
The value of these arguments are 2
, "red"
, c(2, 1)
and c(2, 1)
.
To create these as a "theme", simply create a list object:
# Create own theme
mytheme <- list(d=2, col="red", lty=c(2, 1), lwd=c(2, 1))
In the R console, it will look like this:
> mytheme
$d
[1] 2
$col
[1] "red"
$lty
[1] 2 1
$lwd
[1] 2 1
And to use this theme, rather than calling supergrid()
directly, you should call it within the do.call()
function, examples:
# Plot
x <- 1:10
plot(x)
do.call(supergrid, mytheme)
# Using panel first
plot(x, panel.first=do.call(supergrid, mytheme))
do.call()
works by running the function (specified as first argument), with the arguments specified by the second argument, which should be a list.
You do not need to specify all of the named arguments in your list, as any arguments not specified will instead use the default values.
You can get extra creative with grids, using multiple styles with multiple calls to supergrid()
to create different effects, for example, a 3D or neon effect. Also, as explained in part 1, if you want to use different colours for the vertical and horizontal lines, you would need to call supergrid()
twice.
You can also save these as themes, by creating a list of lists object. Let's use the previous example, except the vertical lines will be blue instead of red.
# Create own theme
mytheme2 <- list(
list(x=NULL, d=2, col="red", lty=c(2, 1), lwd=c(2, 1)),
list(y=NULL, d=2, col="blue", lty=c(2, 1), lwd=c(2, 1)))
In the R console, it will look like this:
> mytheme2
[[1]]
[[1]]$x
NULL
[[1]]$d
[1] 2
[[1]]$col
[1] "red"
[[1]]$lty
[1] 2 1
[[1]]$lwd
[1] 2 1
[[2]]
[[2]]$y
NULL
[[2]]$d
[1] 2
[[2]]$col
[1] "blue"
[[2]]$lty
[1] 2 1
[[2]]$lwd
[1] 2 1
To use this theme, you would need to use lapply()
along with do.call()
, since you need to call supergrid()
twice. Additionally, since supergrid()
will print the output to the console when used with lapply()
, you can also wrap the code inside invisible()
to hide the output... it's starting to sound complicated, but these examples show how it is done:
# Plot
x <- 1:10
plot(x)
invisible(lapply(1:length(mytheme2), \(x) do.call(supergrid, mytheme2[[x]])))
# using panel first
plot(x, panel.first=invisible(lapply(1:length(mytheme2), \(x) do.call(supergrid, mytheme2[[x]]))))
R is a powerful tool for creating graphics, and base R graphics provides everything you need to create some stunning plots and figures. However, always remember that plot order matters!
Built-in themes
supergrid()
comes with some ready-made or built-in themes to use with your plots. To use these themes, you'll need to load the separate script (available on GitHub) in addition to the main supergrid script:
# Load supergrid script from Github
source("https://raw.githubusercontent.com/benbell95/supergrid/refs/heads/main/r/supergrid.r")
# Load themes
source("https://raw.githubusercontent.com/benbell95/supergrid/refs/heads/main/r/supergrid_themes.r")
Or, if you download the code to use locally, change source()
to point to the downloaded file's location. Downloading the code will download all files including the main script, and the themes.
The following themes are available:
ggplot2
Theme similar to the style used in ggplot2 plots. To use:
do.call(supergrid, sg_ggplot2)
lattice
Theme similar to the style used in lattice plots. To use:
do.call(supergrid, sg_lattice)
inverse
Theme which uses inverted colours. To use:
do.call(supergrid, sg_inverse)
circuit
Theme with a style similar to a printed circuit board. To use:
do.call(supergrid, sg_circuit)
ft
Theme with a style similar to plots that appear in the Financial Times newspaper. To use:
do.call(supergrid, sg_ft)
tron
Theme with a grid style similar to the film Tron. To use:
invisible(lapply(1:3, \(x) do.call(supergrid, sg_tron[[x]])))
tron2
Variation on the tron theme. To use:
invisible(lapply(1:3, \(x) do.call(supergrid, sg_tron2[[x]])))
matrix
Theme with a style using the colours of the film The Matrix. To use:
invisible(lapply(1:3, \(x) do.call(supergrid, sg_matrix[[x]])))
Thanks for reading, please leave any comments below, and also share your own themes!
This is a 2 part guide:
Part 1: Supergrid | How to use supergrid() and customise grids for base R plots |
Part 2: Supergrid themes | How to create and use themes with supergrid(), and use built-in themes. |
Further reading
Supergrid GitHub page - Find the function code here.
Quick guide to line types (lty) in R - Guide on how to use different line types in R.
Quick guide to pch symbols in R - Guide on how to use different symbols (pch) in R.
Quick guide to annotating plots in R - Guide on how to annotate plots in R.
No comments
Post a Comment
Comments are moderated. There may be a delay until your comment appears.