Pages (Desktop)

Pages (Mobile)

Supergrid - add custom grids to base R plots

SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID SUPERGRID

Supergrid - a new function to easily add grids to base R plots with major and minor grid lines, fully customisable and pseudo theming for re-use, super easy to use, read on to learn more!

Guide Information

Title Supergrid - add custom grids to base R plots.
Author Benjamin Bell
Published July 31, 2025
Last updated July 31, 2025
R version 4.5.0
Packages base

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.

Supergrid overview

supergrid() is a function to easily add grids to base R plots, which are fully customisable and controllable, with pseudo theming support to easily re-use different styles. But why use supergrid() when R already has the builtin grid() function? Well, supergrid() allows for more fine-tuned control over the placement of the grid, in addition, it can plot minor grid lines between the major grid lines (which appear at the axis tick marks). supergrid() also supports background colours, so if you prefer a dark background with light grid lines, it is easy to do.

Important! supergrid() is designed to work with base graphics. It (ironically) does not support the grid graphics system, which is used by ggplot2, lattice and some other packages for plotting.

Features

  • Quickly and easily add a grid to almost any plot in base R.
  • Major and minor grid lines.
  • Full control over placement of grid lines.
  • Fully customisable, change colour, size, position, etc.
  • Pseudo theming.

How to use

The code is available on my GitHub: https://github.com/benbell95/supergrid, and you can load it directly into R using:

# Load supergrid script from Github
source("https://raw.githubusercontent.com/benbell95/supergrid/refs/heads/main/r/supergrid.r")

Or, you can download the code for a local copy, click the green code icon on GitHub, then download zip. Or just click the icon below. You will still need to source() the downloaded file, just change the location to where you have saved the script on your computer.

Supergrid Download

Once the script is loaded in R, you can use supergrid() to add a grid to any base R plot:

# Data
x <- 1:10
# Plot
plot(x)
# Add supergrid
supergrid()

Which should result in a plot similar to this:

246810246810xy

Since plot order matters in R, and you will most likely want the grid to appear behind other plot elements, you can tweak the usage to ensure this happens, examples are provided in the next sections.

plot()

plot() is the most common function you are likely to use for plotting, since it is a generic function, it can create many different plot types.

To ensure that the grid appears behind other plot elements, there are two different ways which you can achieve this. The easiest way is to use the panel.first argument within plot(), for example:

# Use panel.first (works in most cases)
x <- 1:10
plot(x, panel.first=supergrid())

The panel.first argument plots whatever is specifed first, before the other plot elements, therefore, it will appear behind those plot elements. According to the help file, this argument is a "historical anomaly" since base R plots do not use panels, but "anomaly" or not, it is incredibly useful for arranging plots. However, some plot types may not support this argument.

Another way to arrange the plot order is to create a dummy or null plot. Again, how you do this can vary depending on what you are plotting, but, this will always work (for controlling plot order):

# Null plot (this always works)
x <- 1:10
plot(x, type="n")
supergrid()
points(x)

supergrid() must always be run after the plot code because it requires information generated by the plot function to correctly position the grid. It also requires a graphics device to be running. supergrid() uses the axTicks() function to get the positions of the tick marks, and uses par("usr") to get information about the graphics device plot region. Therefore, if using the null plot method, you must still include the actual data in the plot call to ensure everything is setup correctly. e.g. plot(x) or plot(x, y)

hist() and stripchart()

For histomgrams and stripcharts, you can use the panel.first argument, however, R will display a warning message in the terminal, which you can safely ignore.

# Histogram
x <- rnorm(10)
hist(x, panel.first=supergrid())
# Stripchart
x <- rnorm(50)
stripchart(x, panel.first=supergrid())

barplot() and boxplot()

Bar plots and box plots do not support the panel.first argument, so you must create a null plot first for controlling plot order, then call the plot code again with the add=TRUE argument, examples:

# Barplot example
x <- 1:10
barplot(x)
supergrid()
barplot(x, add=TRUE)
# Boxplot
x <- rnorm(10)
boxplot(x)
supergrid()
boxplot(x, add=TRUE)

Customising the grid

Grid position

supergrid() automatically calculates the position of the grid lines to match the position of the tick marks on the x and y axes, but this assumes you are using the default positions generated by the plot function.

If you are using custom axes for your plot to specify the positions for the tick marks, you would also need to tell these to supergrid() so that it can plot the major grid lines in the right place. You can do this using the x and y arguments, for example:

# Custom axes
x <- 1:100
t <- seq(0, 100, 10)
plot(x, ann=FALSE, axes=FALSE)
supergrid(x=t, y=t)
axis(1, at=t)
axis(2, at=t)

But, you can also use these arguments to control where the grid appears, for example, to show a partial grid on just some of the tick mark locations:

# Partial
x <- 1:10
plot(x)
supergrid(x=c(4,6,8), y=c(6, 8))

In this example, major grid lines will appear on the at positions corresponding to 4, 6, and 8 on the x axis, and 6 and 8 on the y axis. This might be useful if you want to highlight a specific area on your plot. Minor grid lines will still appear on the entire plot region, unless you turn them off, see the next section for details.

If you only want to show grid lines on one of the axes, you can do this by using NULL on either of the x and y arguments. For example, if you only want to show horizontal axis lines (positions corresponding to y axis values), you could use the following code:

# Only draw horizontal grid
x <- 1:10
plot(x)
supergrid(x=NULL)

If you only wanted to show vertical axis lines (positions corresponding to x axis values), you could use the following code:

# Only draw vertical grid
x <- 1:10
plot(x)
supergrid(y=NULL)
246810246810246810246810

Minor grid lines

Minor grid lines are added by default, creating 10 divisions between the major tick mark positions. For example, if the x axis major tick marks are located at 2, 4, 6, and 8, supergrid() will calculate the difference between the first two values, then divide by 10. (4 - 2) / 10 = 0.2 Minor grid lines will then appear at intervals of 0.2, and these intervals are calculated separately for x and y axes.

You can disable minor grid lines by adding the argument minor=FALSE.

x <- 1:10
plot(x)
supergrid(minor=FALSE)

In this example, only the major grid lines will be plotted at the tick mark positions.

You can also change the number of divisions that are created by changing the d argument, for example:

x <- 1:10
plot(x)
# 2 divisions
supergrid(d=2)

# 20 divisions
plot(x)
supergrid(d=20)
246810246810246810246810

Colour, line type and width

supergrid() allows you to change the colour and the alpha channel value of the grid lines, or change the line type or width (thickness), using the col, alp, lty and lwd arguments respectively. If you supply only one value to these arguments, it will apply to both major and minor grid lines.

x <- 1:10
plot(x)
supergrid(col="red", alp=1, lty=1, lwd=3)

If you supply two values to these arguments, the first will apply to the major grid lines, and the second will apply to the minor grid lines.

x <- 1:10
plot(x)
supergrid(col=c("red", "blue"), alp=c(1, 0.5), lty=c(1, 2), lwd=c(3, 1))

This would result in grids similiar to these (here shown with 2 divisions):

246810246810246810246810

You can learn more about different line types and widths, and how to use them in R with my guide: Quick guide to line types (lty) in R.

For the colour argument, you can supply more than two values which will apply the colours across all the grid lines. For example:

# Multiple colour, single alpha value
x <- 1:10
plot(x)
supergrid(col=rainbow(10), alp=1)
2 4 6 8 10 2 4 6 8 10

You can also get quite creative using colour gradients. The following example uses a colour ramp, with the number of graduations matching the number of grid lines. In this case, the grid has been specified, and minor grid lines disabled, in order to achieve the desired effect:

# Gradients
x <- 1:10
g <- seq(0.7,10.4, 0.1)
plot(x)
supergrid(x=g, y=g, col=colorRampPalette(c("limegreen", "gold"))(length(g)), alp=1, minor=FALSE)
2 4 6 8 10 2 4 6 8 10

To change the colour of the x and y grid lines separately, you would need to call supergrid() twice, and suppress one of the axis, for example:

# Different x, y colours
x <- 1:10
plot(x)
supergrid(x=NULL, d=2, col="blue", alp=c(1, 0.5), lwd=c(2, 1))
supergrid(y=NULL, d=2, col="orange", alp=c(1, 0.5), lwd=c(2, 1))
2 4 6 8 10 2 4 6 8 10 Index x

You can also change the background colour (and alpha channel) of the plot, for example, to use lighter grid lines on a darker background. This works by plotting a filled rectangle using rect(), then plotting the grid lines.

When using background colours, it is especially important to ensure that the grid is plotted behind the plot elements, using either the panel.first argument, or a null plot e.g. plot(x, type="n"), otherwise the plot elements will not show.

# Background colour
x <- 1:10
plot(x, col="red", pch=16, panel.first=supergrid(col="white", alp=c(0.7, 0.4), bg="black", alp.bg=0.9))
2 4 6 8 10 2 4 6 8 10

As you can see, there are lots of options to customise the grid, and you can get very creative with your designs, to create some beautiful looking plots.

To make re-use easier, you can save the different style elements as a list object, to learn how to do this, and how to use built in themes, be sure to check out part 2 of this guide!

Thanks for reading, please leave any comments below.

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.
© Benjamin Bell. All Rights Reserved. https://www.benjaminbell.co.uk

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.