Problem with Plots or Graphics Device in the RStudio IDE

Follow

Overview

Occasionally, R graphics will come out distorted or certain parts will be missing (legends, lines, etc.). This is generally due to the plot size or dimensions not being able to properly allocate space for the graphic components. For example, try the following plot:

> plot(mtcars)

Depending on your screen size and plotting region, this plot may look normal or extremely squished. By changing the size of the plotting region, more detail can be hidden or exposed such as labels, text, and points. Using the default R interface (RGui, R.app, or terminal R), graphics are placed in an overlapping window with a relatively large plotting region.

Working with graphics in RStudio

Instead of an overlapping window, graphics created in the RStudio IDE display inside the Plots pane. This is a dedicated region for plots inside the IDE. The width and height of this pane is determined by your current layout. To customize the size of this region, adjust the horizontal and vertical dividers between panes. R will automatically size your graphic to fit inside this region.

Problems while Plotting

Occasionally, distortions or even errors result when plotting:

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

This error indicates that the margins of the particular plot are very large while the region allocated for the plot is too small. You can solve this problem by increasing the size of the plots pane.

2) Graphic with missing or distorted components

When legends, lines, text, or points are missing or "incorrectly" placed, this is often the result of R condensing the plot to fit the region. You can generally solve this by increasing or decreasing the plotting region.

3) Reset your graphics device

Resetting your graphics device will remove any leftover options or settings from previous plots. These might be causing undesired behavior or errors with your current plotting environment. See ?par and ?options for more details. For example:

> plot(cars)
> par(mfrow=c(2,2))
> plot(cars)

To fix this behavior, sometimes it is best to reset your graphics device and then try your plot again. Subsequent plots will use the default graphics settings. To reset your graphics device, call the following code from the console:

> dev.off()

Note:

  • This will delete your current plots in the RStudio Plots Pane.
  • If you have multiple graphics devices open, repeat this command until the output displays null device.

Further Help

If the above approaches do not solve your problem, try reproducing outside of RStudio. Use the default interface installed with R such as RGui, R.app, or terminal R. If the same problem occurs without using RStudio, the error is related to R or the R code. There are various ways for getting help with R.

If this problem only exists in RStudio, then please open a new discussion to let us know. The best way for us to track down an error is for us to reproduce it locally. First simplify your code into a short reproducible example. Then send us all the necessary code and information so we can reproduce it as well. Make sure to include the output of the following:

> sessionInfo()
> RStudio.Version()

Ask for help on community.rstudio.com

You may also ask for help from R and RStudio users on community.rstudio.com. Be sure to include a reproducible example of your issue. To start a new community discussion, click here.

Return to Troubleshooting Guide: Using RStudio

Comments