How to Solve R Error in plot.new(): figure margins too large

by | Programming, R, Tips

When working with plots in R, you can face the error: Error in plot.new() figure margins too large. This error occurs when there is not enough space for the plot due to the size of the margins compared to the plotting window. Fortunately, there are straightforward solutions to this problem!

In this blog post we will go through the causes of the error, an example to reproduce the error and step-by-step solutions with R code examples.

Let’s get started!


Causes of the Error

The error occurs when:

  1. The plotting window is too small: When the plot area is too small for the plot’s margins, R fails to draw the plot.
  2. Large margin settings: Margins can be explicitly set too large, leaving insufficient room for the plot.

Example to Replicate the Error

Here is an example of huge margins that will cause the plot.new(): figure margins too large error.

# Set very large margins
par(mar = c(100, 100, 100, 100))  # bottom, left, top, right

# Attempt to plot
plot(cars)

In the above code:

  • The par() function sets the huge margins with the mar parameter.
  • When you try to plot using plot(cars), R cannot assign enough space for the plot due to these large margins, resulting in the error.

Now for the solution!

Solution 1: Adjust the mar Parameter

R uses the mar graphical parameter to control the size of margins. The mar argument is a numeric vector of length 4, which specifies the size of the margins in this order:

  • Bottom
  • Left
  • Top
  • Right

Sometimes, margins are set too large relative to the plotting area. You can adjust this setting by modifying the mar parameter.

Steps to Fix:

The par() function can set smaller margins before calling your plot function.

# Reduce the margins
par(mar = c(4, 4, 2, 2))  # bottom, left, top, right
plot(cars)

This will reduce the margin sizes, making more room for the plot to render correctly.

Solution 2: Set a Specific Graphic Device Size

Another way to avoid this error is to explicitly set the graphic device size when using functions like pdf(), png(), or jpeg().

Example:

# Set a larger graphic device size
pdf("plot_output.pdf", width = 10, height = 8)
plot(cars)
dev.off()

This ensures that the graphic device is large enough to accommodate the plot and its margins.

Solution 3: Reset Default Graphical Parameters

If you have earlier experimented with custom graphical parameters (like margins or layout settings), it may be helpful to reset them to their default values. The par() function can do this.

Example:

# Reset graphical parameters to defaults
dev.off()  # This resets all graphical parameters
plot(cars)

Alternatively, if you’re using a multi-panel layout (i.e., dividing the plotting area into multiple subplots), you may have set the mfrow or mfcol parameters, which divide the plot area into rows and columns. These settings can sometimes cause the error due to insufficient space for each plot.

Understanding mfrow and mfcol in Multi-Panel Layouts

The par() function allows you to split the plotting window into multiple subplots using the mfrow and mfcol parameters:

  • mfrow: Specifies the number of rows and columns in which plots will be drawn. It fills plots by row.
  • mfcol: Similar to mfrow, but fills plots by column.

If you set these parameters to create multiple plots in the same window, and if there’s not enough space to accommodate them all, you might face the figure margins too large error.

Example of Using mfrow to Cause the Error:

# Set up a 2x2 plotting layout with large margins
par(mfrow = c(2, 2), mar = c(100, 100, 100, 100))

# Try to plot multiple graphs
plot(cars)
plot(cars)
plot(cars)
plot(cars)

In this example, we try to plot 4 graphs in a 2×2 grid, but with the margins set too large (mar = c(100, 100, 100, 100)), the available space for each plot is too small, resulting in the error.

Solution:

Reduce the margins and try again.

# Set up a 2x2 plotting layout with smaller margins
par(mfrow = c(2, 2), mar = c(4, 4, 2, 2))

# Now plot again
plot(cars)
plot(cars)
plot(cars)
plot(cars)

Here, we use a 2×2 layout (mfrow = c(2, 2)) but reduce the margins to a reasonable size (mar = c(4, 4, 2, 2)), which should solve the problem.

Solution 4: Resize the Plotting Window

In R, the size of the plotting window is determined by either the graphical parameters or the device you’re using to view the plots. For instance, if you’re working in RStudio, the plotting window in the lower right panel might be too small.

Steps to Fix:

  • Increase the size of the plotting window by dragging its corner to expand.
  • If you’re using an external plotting device (e.g., pdf(), png()), adjust the dimensions specified when opening the device.

Example:

If you’re working in RStudio and encounter this error, expand the plotting window in the lower-right corner. Then try to re-run your plotting code:

plot(cars)

Conclusion

The error plot.new(): figure margins too large is fairly common when working with plots in R, but it’s easily resolved by adjusting either the plotting window size, margin settings, or the plotting device. If you follow the solutions outlined in this post, you should be able to remove the error and continue working with your plots smoothly.

Key Takeaways:

  • Resize your plotting window in RStudio or any other environment.
  • Adjust the mar parameter to make margins smaller.
  • Set larger plotting device sizes for external devices.
  • Reset graphical parameters when necessary, especially when using multi-panel layouts with mfrow or mfcol.

Try out these solutions the next time you face this error, and happy plotting!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee

This website collects data via Google Analytics. Click here to opt in. Click here to opt out. ?