How to Fix: error in plot.new() : figure margins too large


One error you may encounter in R is:

Error in plot.new() : figure margins too large

This error occurs when the plot panel in RStudio is too small for the margins of the plot that you’re attempting to create.

This tutorial shares exactly how to fix this error.

How to Reproduce the Error

Suppose we attempt to create the following plot in R:

#attempt to create scatterplot
plot(1:30)

We receive the following error:

Error in plot.new() : figure margins too large 

We receive this error because the plot panel is extremely small (notice how small the panel is in the bottom left corner) and so the margins of the plot can’t be displayed in such a small panel.

Method #1: Fix the Error by Increasing the Size of the Plot Panel

The easiest way to fix this error is to increase the size of the plotting panel in RStudio:

plot(1:30)

Notice that we don’t receive an error because the plotting panel was large enough to display the margins of the plot.

Method #2: Fix the Error by Using the par() Function

By default, the par() function in R sets the margins of a plot as follows:

  • Bottom margin: 5.1
  • Left margin: 4.1
  • Top margin: 4.1
  • Right margin: 2.1

However, we can use the following syntax to make the margins smaller:

#adjust plot margins
par(mar = c(1, 1, 1, 1))

#create scatterplot
plot(1:30)

The plot is successfully displayed in the plotting panel in RStudio because we reduced the margins so much.

Method #3: Fix the Error by Shutting Down Current Plotting Device

If neither of the previous methods are able to fix the error, then you may need to use the following code to shut down the current plotting device:

dev.off()

In some cases, this may fix the error because it removes any plot settings that were used for previous plots and may be interfering with your current plot.

Additional Resources

The following tutorials explain how to perform other common plotting functions in R:

How to Use the par() Function in R
How to Overlay Plots in R
How to Save Multiple Plots to PDF in R

Leave a Reply

Your email address will not be published.