How to Break the Axis of Plots in R


Occasionally you may want to break the axis of a plot in R to display data values of different orders of magnitude on the same plot.

The easiest way to do this is by using the gap.plot() function from the plotrix package in R.

This function uses the following basic syntax:

gap.plot(x, y, gap, gap.axis=”y”, …)

where:

  • x: A vector of x-values.
  • y: A vector of y-values.
  • gap: The range of values to be left out.
  • gap.axis: Whether the gaps should be on the x or y axis.

Note that the default behavior for the gap.plot() function is to use the y-axis as the break axis, but it’s possible to specify gap.axis=”x” to instead break the x-axis.

The following examples show how to use this function to break the x-axis and y-axis of plots in R.

Example 1: How to Break the X-Axis of Plot in R

Suppose that we would like to create a plot of x vs. y points in R and create a break on the x-axis between the x values of 30 and 60.

We can use the following syntax to do so:

library(plotrix)
  
#define x and y vectors 
x <- 1:100 
y <- 1:100 
  
#create plot of x vs. y with gap on x-axis between 30 and 60
gap.plot(x, y, gap=c(30, 60), gap.axis='x', col='blue') 

 

Break x-axis of plot in R

Notice that the plot has a break on the x-axis between the points of 30 and 60, just as we specified.

Note: If you receive an error when using the gap.plot() function, you may need to first install the plotrix package by using install.packages(‘plotrix’) before running this code.

Example 2: How to Break the Y-Axis of Plot in R

Suppose that we would like to create a plot of x vs. y points in R and create a break on the y-axis between the y values of 30 and 60.

We can use the following syntax to do so:

library(plotrix)
  
#define x and y vectors 
x <- 1:100 
y <- 1:100 
  
#create plot of x vs. y with gap on y-axis between 30 and 60
gap.plot(x, y, gap=c(30, 60), col='blue') 

Break y-axis on plot in R

Notice that the plot has a break on the y-axis between the points of 30 and 60, just as we specified.

Note that in this example we didn’t have to specify gap.axis=’y’ because this is the default behavior of the gap.plot() function.

Also note that it’s possible to customize the plot using arguments from base R plots, such as main, xlab and ylab to specify the plot title and axis titles:

library(plotrix)
  
#define x and y vectors 
x <- 1:100 
y <- 1:100 
  
#create plot of x vs. y with gap on y-axis between 30 and 60
gap.plot(x, y, gap=c(30, 60), col='blue', main='Axis Break on Y-Axis',
xlab='X Values',
ylab='Y Values')

This produces the following plot:

Notice that a custom title and axis labels are now shown in the plot.

Feel free to use whatever arguments you would like from base R to customize the plot even further.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Change X-Axis Labels of Barplot in R
How to Order Items on x-axis in ggplot2
How to Reverse Order of Axis in ggplot2

Leave a Reply

Your email address will not be published. Required fields are marked *