Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot arrange multiple R plots in same plotting space with par #3562

Open
Tracked by #4734
timtmok opened this issue Jun 17, 2024 · 7 comments
Open
Tracked by #4734

Cannot arrange multiple R plots in same plotting space with par #3562

timtmok opened this issue Jun 17, 2024 · 7 comments
Labels
area: plots Issues related to Plots category. bug Something isn't working lang: r support

Comments

@timtmok
Copy link
Contributor

timtmok commented Jun 17, 2024

Positron Version:

Positron Version: 2024.06.0 (Universal) build 770
Code - OSS Version: 1.90.0
Commit: 5d79ea6
Date: 2024-06-14T05:53:04.122Z
Electron: 29.4.0
Chromium: 122.0.6261.156
Node.js: 20.9.0
V8: 12.2.281.27-electron.0
OS: Darwin arm64 23.4.0

R 4.4.0

Steps to reproduce the issue:

  1. Run reprex

Reprex:

par(mfrow=c(3,1))

hist(iris$Petal.Width [iris$Species == "setosa"],
     xlim = c(0,3),
     breaks = 9,
     main = "Petal Width for Setosa",
     xlab = "",
     col = "red")

hist(iris$Petal.Width [iris$Species == "virginica"],
     xlim = c(0,3),
     breaks = 9,
     main = "Petal Width for Virginica",
     xlab = "",
     col = "green")

hist(iris$Petal.Width [iris$Species == "versicolor"],
     xlim = c(0,3),
     breaks = 9,
     main = "Petal Width for Versicolor",
     xlab = "",
     col = "blue")

par(mfrow=c(1,1)) # reset layout

This usually happens in RStudio if the plots pane is too small. Enlarging the plots view in Positron doesn't seem to help. Using par(mfrow=c(1,3)) does work if the plots view is wide enough but doesn't always update to show the last plot. It does fail if setting a custom plot size of 100x100. A larger custom plot size still has an error with the reprex.

What did you expect to happen?

All three plots render in the same plotting space like this:
image

Were there any error messages in the output or Developer Tools console?

Error in `plot.new()`:
! figure margins too large
Hide Traceback1. ├─graphics::hist(...)
 2. └─graphics::hist.default(...)
 3.   ├─base::plot(...)
 4.   └─graphics:::plot.histogram(...)
 5.     └─graphics::plot.new()
@timtmok timtmok added bug Something isn't working area: plots Issues related to Plots category. lang: r labels Jun 17, 2024
@petetronic petetronic added this to the Release Candidate milestone Jun 19, 2024
@jmcphers
Copy link
Collaborator

@edzer
Copy link

edzer commented Jul 11, 2024

I think this is related:

plt2 = function() {
  layout(matrix(1:2, 2))
  plot(1,1)
  plot(1,1)
}
plt2()

gives an error:

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

although this works in base R and Rstudio. It prevents plotting maps in packages sf and terra.

@chad-fisher
Copy link

I'm also seeing this issue when I try to make a bunch of histograms in the same figure using mfrow. I'm still pretty new to all this so let me know if you need my code or anything, but I've confirmed it behaves as expected in RMD in RStudio but not in Positron

@jmcphers
Copy link
Collaborator

jmcphers commented Sep 5, 2024

Reported again in #4582.

@arnaldmarcer
Copy link

arnaldmarcer commented Sep 6, 2024

Yes, I reported #4582. Here is the plot I get to the code

par(mfrow=c(1,2))
plot(rnorm(100))
plot(rnorm(1000))

image

No error, just second plot is not shown although the space for it is there. Also, doesn´t get solved by resizing the plot area.

@arnaldmarcer
Copy link

Just discovered this which might help. It is the last plot that is not shown. For instance, by doing

par(mfrow=c(1,3))
plot(rnorm(10))
plot(rnorm(100))
plot(rnorm(1000))

image

only the last one is not shown.

Same here:

par(mfrow=c(2,2))
plot(rnorm(10))
plot(rnorm(100))
plot(rnorm(1000))
plot(rnorm(10000))

image

Hope this helps

@DavisVaughan DavisVaughan mentioned this issue Sep 18, 2024
10 tasks
@grantmcdermott
Copy link

grantmcdermott commented Oct 8, 2024

Interestingly, when I use tinyplot to compose multiplot arrangements, things work fine. E.g. This chunk produces the equivalent result to @timtmok's original post (and displays correctly in Positron's PLOTS pane).

library(tinyplot)

plt(
  ~ Petal.Width | Species,
  data       = iris,
  type       = 'hist',
  facet      = 'by',  # facet along "by" grouping varible (here: Species)
  facet.args = list(ncol = 1),
  legend     = FALSE,
  frame      = FALSE  # optional
)

Image

The reason I bring this up is that it suggests (to me, at least) that the figure margins to large error is possibly a misnomer. What tinyplot does internally is open the plot window with plot.new, then draw common main and axes titles in the outer plot margins, and then use mfrow to draw the individual "facets" in the remaining space (i.e., after accounting for outer margins). So it's basically the same process as the manual approach, but has somehow tricked Positron into thinking there is enough space to draw everything.

(Legends are another issue because of internal margin (i.e., par("mar")) adjustments, but that is being tracked separately in #3667.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: plots Issues related to Plots category. bug Something isn't working lang: r support
Projects
None yet
Development

No branches or pull requests

8 participants