Table of Contents
The error “need finite ‘xlim’ values” in the plot.window() function indicates that there is an issue with the specified x-axis limits for the plot. This could be caused by providing non-numerical values or values that are too large or too small to be plotted. It is important to ensure that the xlim values are valid and within the range of the data being plotted in order to avoid this error.
Fix: Error in plot.window(…) : need finite ‘xlim’ values
One error you may encounter when using R is:
Error in plot.window(...) : need finite 'xlim' values
This error occurs when you attempt to create a plot in R and use either a character vector or a vector with only NA values on the x-axis.
The following examples show two different scenarios where this error may occur in practice.
Example 1: Error with Character Vector
Suppose attempt to create a scatterplot using the following code:
#define data
x <- c('A', 'B', 'C', 'D', 'E', 'F')
y <- c(3, 6, 7, 8, 14, 19)
#attempt to create scatterplot
plot(x, y)
Error in plot.window(...) : need finite 'xlim' values
We receive an error because the vector that we used for the x-axis values is a character vector.
To fix this error, we simply need to supply a numeric vector to the x-axis:
#define two numeric vectors
x <- c(1, 2, 3, 4, 5, 6)
y <- c(3, 6, 7, 8, 14, 19)
#create scatterplot
plot(x, y)
We’re able to create the scatterplot without any errors because we supplied a numeric vector for the x-axis.
Example 2: Error with Vector of NA Values
Suppose attempt to create a scatterplot using the following code:
#define data
x <- c(NA, NA, NA, NA, NA, NA)
y <- c(3, 6, 7, 8, 14, 19)
#attempt to create scatterplot
plot(x, y)
Error in plot.window(...) : need finite 'xlim' values
We receive an error because the vector that we used for the x-axis values is a vector with only NA values.
To fix this error, we simply need to supply a numeric vector to the x-axis:
#define two numeric vectors
x <- c(1, 5, 9, 13, 19, 22)
y <- c(3, 6, 7, 8, 14, 19)
#create scatterplot
plot(x, y)
Once again we’re able to successfully create a scatterplot with no errors because we used a numeric vector for the x-axis.
The following tutorials explain how to fix other common errors in R:
Cite this article
stats writer (2024). What could be causing the error “need finite ‘xlim’ values” in the plot.window() function?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-could-be-causing-the-error-need-finite-xlim-values-in-the-plot-window-function/
stats writer. "What could be causing the error “need finite ‘xlim’ values” in the plot.window() function?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/what-could-be-causing-the-error-need-finite-xlim-values-in-the-plot-window-function/.
stats writer. "What could be causing the error “need finite ‘xlim’ values” in the plot.window() function?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-could-be-causing-the-error-need-finite-xlim-values-in-the-plot-window-function/.
stats writer (2024) 'What could be causing the error “need finite ‘xlim’ values” in the plot.window() function?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-could-be-causing-the-error-need-finite-xlim-values-in-the-plot-window-function/.
[1] stats writer, "What could be causing the error “need finite ‘xlim’ values” in the plot.window() function?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. What could be causing the error “need finite ‘xlim’ values” in the plot.window() function?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
