R Error: error in plot.window(…) : need finite ‘xlim’ values

Getting error messages is a common part of programming, they are annoying but they will happen, particularly as programs get complex. The “error in plot.window(…) : need finite ‘xlim’ values” error message is an easy one to make when plotting data structures. It can occur if you are not paying attention to the values in the data structures that you are using.

The circumstances of this error.

The circumstances of this error are found in the use of the plot() function. The plot() function is the most basic graphing function in R and it is the easiest one to master. However, it is not without its potential making mistakes.

> x = c(1,2,3,4,5)
> y = c(2,3,4,5,6)
> q = c(“a”,”b”,”c”,”d”, “e”)
> plot(x,q)
Error in plot.window(…) : need finite ‘ylim’ values

As you can see in this example, the vectors entered into the plot() function are “x” and “q” with “x” being a numeric vector and “q” a character Victor. Note the unused numeric vector “y” which is also a numeric vector.

What is causing this error?

The reason for this area is very simple, if you are getting it, you are putting a character vector where a numeric vector that there should be. The plot() function only uses numeric values and this causes it to produce our error message.

x = c(1,2,3,4,5)
y = c(2,3,4,5,6)
q = c(“a”,”b”,”c”,”d”, “e”)
plot(x,y)

In this example, we use the two numeric vectors in the plot() function and it goes ahead and plots the graph without any errors. The key to properly using this function is to understand that it only uses numeric values and if there is any attempt to use characters that will produce an error.

How to fix this error.

Fixing this error message is quite simple once you know what causes it. It is a simple matter of making sure that the values you are entering into the plot() function are numeric values, characters who always give you some form of an error message.

> x = c(1,2,3,4,5)
> y = c(2,3,4,5,6)
> q = c(“a”,”b”,”c”,”d”, “e”)
> plot(x,q)

> x = c(1,2,3,4,5)
> y = c(2,3,4,5,6)
> q = c(“a”,”b”,”c”,”d”, “e”)
> plot(x,y)

A close look at both of our examples shows the difference “q” is a character vector and “y” is a numeric vector. That is where the key to fixing this problem exists.

> plot(x,q)
> plot(x,y)

As seen in this side-by-side comparison of the two example plot() functions that fixing this particular case is swapping the “y” for the “q” in the plot() function. In other cases, the fix may be more complex such as flawed labeling in a data frame, but the basic solution is still the same. Ensure that the values you are entering into the plot() function are numeric, and you will have fixed this error message.

Scroll to top
Privacy Policy