Table of Contents
The xlim() and ylim() functions in R allow for the customization of the x- and y-axes limits on a plot. This can be useful for adjusting the range of data displayed or for highlighting specific areas of interest on the plot. By specifying the desired limits, the user can control the scale and focus of the plot, providing a more accurate and informative visual representation of the data. These functions are simple and efficient tools for creating customized plots in R.
Use xlim() and ylim() in R
You can use the xlim() and ylim() functions to set the x-axis limits and y-axis limits of plots in R.
The following examples show how to use these functions in practice.
Example 1: Use xlim() to Set X-Axis Limits
The following code shows how to create a scatterplot in R and specify the x-axis limits using the xlim() function:
#define data frame df <- data.frame(x=c(1, 3, 4, 5, 7, 9), y=c(7, 7, 8, 12, 15, 19)) #create scatterplot with x-axis limits ranging from 0 to 20 plot(df$x, df$y, pch=19, xlim=c(0, 20))

Example 2: Use ylim() to Set Y-Axis Limits
The following code shows how to create a scatterplot in R and specify the y-axis limits using the ylim() function:
#define data frame df <- data.frame(x=c(1, 3, 4, 5, 7, 9), y=c(7, 7, 8, 12, 15, 19)) #create scatterplot with y-axis limits ranging from 0 to 30 plot(df$x, df$y, pch=19, ylim=c(0, 30))

Example 3: Use xlim() & ylim() to Set Axis Limits
The following code shows how to create a scatterplot in R and specify both the x-axis limits and the y-axis limits:
#define data frame df <- data.frame(x=c(1, 3, 4, 5, 7, 9), y=c(7, 7, 8, 12, 15, 19)) #create scatterplot and specify both x-axis limits and y-axis limits plot(df$x, df$y, pch=19, xlim=c(0, 20), ylim=c(0, 30))

Cite this article
stats writer (2024). How can I use xlim() and ylim() in R to set the limits for the x- and y-axes of a plot?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-xlim-and-ylim-in-r-to-set-the-limits-for-the-x-and-y-axes-of-a-plot/
stats writer. "How can I use xlim() and ylim() in R to set the limits for the x- and y-axes of a plot?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-xlim-and-ylim-in-r-to-set-the-limits-for-the-x-and-y-axes-of-a-plot/.
stats writer. "How can I use xlim() and ylim() in R to set the limits for the x- and y-axes of a plot?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-xlim-and-ylim-in-r-to-set-the-limits-for-the-x-and-y-axes-of-a-plot/.
stats writer (2024) 'How can I use xlim() and ylim() in R to set the limits for the x- and y-axes of a plot?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-xlim-and-ylim-in-r-to-set-the-limits-for-the-x-and-y-axes-of-a-plot/.
[1] stats writer, "How can I use xlim() and ylim() in R to set the limits for the x- and y-axes of a plot?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I use xlim() and ylim() in R to set the limits for the x- and y-axes of a plot?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
