Table of Contents
The scale_y_continuous function is a powerful tool in ggplot2 that allows users to adjust the scale of the y-axis in their graphs. This function can be utilized by specifying the desired range of values for the y-axis, as well as any other desired formatting options, such as labels and breaks.
For example, if we have a scatter plot with y-values ranging from 0 to 100, but we want to focus on the data points between 20 and 80, we can use scale_y_continuous to set the y-axis limits to 20 and 80. This will zoom in on the relevant data and make it easier to analyze.
Another way to use scale_y_continuous is to adjust the axis breaks and labels. For instance, if our y-values are in millions, we can use the function to set breaks at every million and add a “M” label to the end of each number. This allows for a more concise and understandable presentation of the data.
Overall, the scale_y_continuous function is a versatile tool that allows for customization of the y-axis scale in ggplot2. By specifying the desired range, breaks, and labels, users can effectively adjust the y-axis to best represent their data and enhance their visualizations.
Use scale_y_continuous in ggplot2 (With Examples)
You can use the scale_y_continuous() function in ggplot2 to customize the y-axis of a given plot.
This function uses the following basic syntax:
p +
scale_y_continuous(breaks, n.breaks, labels, limits, ...)
where:
- breaks: A numeric vector of positions for breaks on the y-axis
- n.breaks: An integer vector specifying the number of total breaks on the y-axis
- labels: A character vector of labels to use for the y-axis
- limits: A numeric vector that specifies the min and max value for the y-axis
The following examples show how to use this function in different scenarios with the following data frame in R:
#create data frame df <- data.frame(points=c(5, 7, 12, 13, 15, 19, 22, 25), assists=c(4, 3, 2, 3, 7, 8, 5, 7)) #view data frame df points assists 1 5 4 2 7 3 3 12 2 4 13 3 5 15 7 6 19 8 7 22 5 8 25 7
Example 1: Use scale_y_continuous with Custom Axis Breaks
The following code shows how to create a scatterplot in ggplot2 and use scale_y_continuous() with the breaks argument to specify custom axis breaks of 2, 5 and 8:
library(ggplot2)
#create scatterplot with custom y-axis breaks
ggplot(df, aes(x=points, y=assists)) +
geom_point(size=2) +
scale_y_continuous(breaks=c(2, 5, 8))
Notice that the y-axis only contains axis breaks at 2, 5 and 8, just as we specified using the breaks argument.
Example 2: Use scale_y_continuous with Custom Number of Breaks
The following code shows how to create a scatterplot in ggplot2 and use scale_y_continuous() with the n.breaks argument to place exactly 2 axis breaks on the y-axis:
library(ggplot2)
#create scatterplot with custom number of breaks on y-axis
ggplot(df, aes(x=points, y=assists)) +
geom_point(size=2) +
scale_y_continuous(n.breaks=2)
Notice that the y-axis contains exactly 2 axis breaks, just as we specified using the n.breaks argument.
Example 3: Use scale_y_continuous with Custom Labels
The following code shows how to create a scatterplot in ggplot2 and use scale_y_continuous() with the labels argument to specify the label names to place on the y-axis:
library(ggplot2)
#create scatterplot with custom labels
ggplot(df, aes(x=points, y=assists)) +
geom_point(size=2) +
scale_y_continuous(breaks=c(2, 5, 8), labels=c('two', 'five', 'eight'))
Notice that the y-axis contains 3 axis breaks each with custom labels, just as we specified using the labels argument.
Example 4: Use scale_y_continuous with Custom Limits
The following code shows how to create a scatterplot in ggplot2 and use scale_y_continuous() with the limits argument to specify custom y-axis limits of 0 and 20:
library(ggplot2)
#create scatterplot with custom y-axis limits
ggplot(df, aes(x=points, y=assists)) +
geom_point(size=2) +
scale_y_continuous(limits=c(0, 20))
Notice that the y-axis ranges from 0 to 20, just as we specified using the limits argument.
The following tutorials explain how to perform other common tasks in ggplot2:
Cite this article
stats writer (2024). How can the scale_y_continuous function be utilized in ggplot2 to adjust the y-axis scale? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-scale_y_continuous-function-be-utilized-in-ggplot2-to-adjust-the-y-axis-scale-can-you-provide-some-examples/
stats writer. "How can the scale_y_continuous function be utilized in ggplot2 to adjust the y-axis scale? Can you provide some examples?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-scale_y_continuous-function-be-utilized-in-ggplot2-to-adjust-the-y-axis-scale-can-you-provide-some-examples/.
stats writer. "How can the scale_y_continuous function be utilized in ggplot2 to adjust the y-axis scale? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-scale_y_continuous-function-be-utilized-in-ggplot2-to-adjust-the-y-axis-scale-can-you-provide-some-examples/.
stats writer (2024) 'How can the scale_y_continuous function be utilized in ggplot2 to adjust the y-axis scale? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-scale_y_continuous-function-be-utilized-in-ggplot2-to-adjust-the-y-axis-scale-can-you-provide-some-examples/.
[1] stats writer, "How can the scale_y_continuous function be utilized in ggplot2 to adjust the y-axis scale? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can the scale_y_continuous function be utilized in ggplot2 to adjust the y-axis scale? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
