Table of Contents
GGplot2 is a widely used data visualization tool that allows users to create customizable and visually appealing graphs. One of the key features of ggplot2 is the ability to set axis breaks, which allows for better control over the appearance of the graph. Axis breaks are points on the axis where the scale changes, typically to accommodate large or small values. This can be useful in cases where the data has extreme values that make it difficult to visualize patterns in the rest of the data. Some examples of setting axis breaks in ggplot2 include using the “scale_y_continuous” function to specify a custom range and the “scale_x_continuous” function to set breaks at specific values. Another example is using the “scale_y_log10” function to create a logarithmic scale for the y-axis. Overall, setting axis breaks in ggplot2 allows for more flexibility in displaying and interpreting data in a visually appealing manner.
Set Axis Breaks in ggplot2 (With Examples)
You can use the following syntax to set the axis breaks for the y-axis and x-axis in :
#set breaks on y-axis scale_y_continuous(limits = c(0, 100), breaks = c(0, 50, 100)) #set breaks on y-axis scale_x_continuous(limits = c(0, 10), breaks = c(0, 2, 4, 6, 8, 10))
The following examples show how to use this syntax in practice with the following data frame:
#create data frame df <- data.frame(x=c(1, 2, 4, 5, 7, 8, 9, 10), y=c(12, 17, 27, 39, 50, 57, 66, 80)) #view data frame df x y 1 1 12 2 2 17 3 4 27 4 5 39 5 7 50 6 8 57 7 9 66 8 10 80
Example 1: Set Y-Axis Breaks
The following code shows how to create a simple scatterplot using ggplot2:
library(ggplot2)#create scatterplot of x vs. y ggplot(df, aes(x=x, y=y)) + geom_point()

By default, the y-axis shows breaks at 20, 40, 60, and 80. However, we can use the scale_y_continuous() function to display breaks at every 10 values instead:
#create scatterplot of x vs. y with custom breaks on y-axis
ggplot(df, aes(x=x, y=y)) +
geom_point() +
scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, 10))
Example 2: Set X-Axis Breaks
We can use the scale_x_continuous() function to set the breaks on the x-axis:
#create scatterplot of x vs. y with custom breaks on x-axis
ggplot(df, aes(x=x, y=y)) +
geom_point() +
scale_x_continuous(limits = c(0, 10), breaks = c(0, 2, 4, 6, 8, 10))
We typically set axis breaks at uniform intervals, but we could choose to set axis breaks only at specific numbers.
For example, the following code shows how to display x-axis breaks only at the values 0, 7, and 10:
#create scatterplot of x vs. y with custom breaks on x-axis
ggplot(df, aes(x=x, y=y)) +
geom_point() +
scale_x_continuous(limits = c(0, 10), breaks = c(0, 7, 10))
The following tutorials show how to perform other common operations in ggplot2:
Cite this article
stats writer (2024). How can I set axis breaks in ggplot2, and what are some examples of doing so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-set-axis-breaks-in-ggplot2-and-what-are-some-examples-of-doing-so/
stats writer. "How can I set axis breaks in ggplot2, and what are some examples of doing so?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-set-axis-breaks-in-ggplot2-and-what-are-some-examples-of-doing-so/.
stats writer. "How can I set axis breaks in ggplot2, and what are some examples of doing so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-set-axis-breaks-in-ggplot2-and-what-are-some-examples-of-doing-so/.
stats writer (2024) 'How can I set axis breaks in ggplot2, and what are some examples of doing so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-set-axis-breaks-in-ggplot2-and-what-are-some-examples-of-doing-so/.
[1] stats writer, "How can I set axis breaks in ggplot2, and what are some examples of doing so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I set axis breaks in ggplot2, and what are some examples of doing so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
