In plot.window(…) : nonfinite axis limits [GScale(-inf,1,1, .); log=1]
In plot.window(…) : nonfinite axis limits [GScale(-inf,1,1, .); log=1], Understanding and Fixing Log-Scale Axis Limits in R Plots
When working with log-scale plots in R, you might encounter warning messages like:
Warning messages:
In plot.window(…) : nonfinite axis limits [GScale(-inf,1,1, .); log=1]
This can be confusing at first, especially if you’re trying to manually set axis limits with xlim
and ylim
.
In plot.window(…) : nonfinite axis limits [GScale(-inf,1,1, .); log=1]
In this article, we’ll explore why these warnings occur and how to properly specify axis limits for log-scale plots in base R.
Why Do These Warnings Occur?
Suppose you want to create a plot with logarithmic axes and set custom limits, like so:
plot(1:10, log = "xy", xlim = c(0, 10), ylim = c(0, 10))
You might expect this to work smoothly, but instead, R throws warning messages:
Warning messages:
In plot.window(…) : nonfinite axis limits [GScale(-inf,1,1, .); log=1]
The core issue here is setting the lower axis limits to zero (0
) on a logarithmic scale.
Since the log of zero is undefined (minus infinity), R interprets this as an invalid limit, causing the warning and resulting in a plot that doesn’t display as expected.
How to Correctly Set Limits on Log-Scale Plots
Key Point: When plotting on a log scale, all axis limits must be positive numbers greater than zero. Zero or negative values are invalid because their logarithm is undefined.
Example 1: Reproducing the Warning
Here’s an example that triggers the warning:
plot(1:10, log = "xy", xlim = c(0, 10), ylim = c(0, 10))
The lower limits (0
) cause the warning because log(0)
is undefined.
Example 2: Fixing the Warning
To fix this, set the xlim
and ylim
to values slightly above zero:
plot(1:10,
log = "xy",
xlim = c(0.001, 10),
ylim = c(0.001, 10))
This way, the limits are positive, and R can properly compute the logarithm of these bounds.
Best Practices for Log-Scale Plot Limits
- Always set
xlim
andylim
to positive values greater than zero. - Avoid using zero or negative numbers for limits on log-scale plots.
- Choose a small positive number (like
0.001
) to represent limits close to zero if needed.
Summary
When creating log-scale plots in R:
- Invalid:
xlim = c(0, 10)
orylim = c(0, 10)
- Valid:
xlim = c(0.001, 10)
andylim = c(0.001, 10)
By following these guidelines, you’ll prevent warning messages and ensure your plots display correctly.