How to fix error in plot.window(…) : need finite ‘xlim’ values?

The error ‘need finite ‘xlim’ values’ occurs when the x-limits of the plot window are not finite. To fix this, you need to specify finite x-limits in the plot window function. This can be done by using the ‘xlim’ argument in the plot window function and setting the x-limits with values that are within the range of the data being plotted. Once this is done, the error should be resolved.


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:

x