Skip to content

"Copy as metafile" in Rgui, as well as default ggsave(), scale the whole figure to the top left quarter #6349

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

Closed
telenskyt opened this issue Feb 28, 2025 · 5 comments

Comments

@telenskyt
Copy link

I have exactly the same problem as reported here, not sure why that one was closed.

When I produce ggplot2 and then right click the chart and select "Copy as metafile" (in the standard Rgui in Windows), it scales the whole figure just in the top left quarter like this:

Image

  1. In the Rgui, everything looks alright.
  2. I get the same faulty result when I do ggsave("cover.wmf").

Reproducible code:

cover <- structure(list(scen = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 
12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 
14L, 15L, 15L, 15L, 15L, 15L, 15L), n = c(20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20), r = c(0, 0, 0, 0, 0, 0, 0.2, 0.2, 0.2, 0.2, 
0.2, 0.2, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.6, 0.6, 0.6, 0.6, 0.6, 
0.6, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0, 0, 0, 0, 0, 0, 0.2, 0.2, 
0.2, 0.2, 0.2, 0.2, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.6, 0.6, 0.6, 
0.6, 0.6, 0.6, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8, 0, 0, 0, 0, 0, 0, 
0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.6, 
0.6, 0.6, 0.6, 0.6, 0.6, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8), n_ind = c(40, 
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 160, 160, 
160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 
160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 
160, 160, 640, 640, 640, 640, 640, 640, 640, 640, 640, 640, 640, 
640, 640, 640, 640, 640, 640, 640, 640, 640, 640, 640, 640, 640, 
640, 640, 640, 640, 640, 640), method = structure(c(3L, 4L, 5L, 
2L, 6L, 1L, 3L, 4L, 5L, 2L, 6L, 1L, 3L, 4L, 5L, 2L, 6L, 1L, 3L, 
4L, 5L, 2L, 6L, 1L, 3L, 4L, 5L, 2L, 6L, 1L, 3L, 4L, 5L, 2L, 6L, 
1L, 3L, 4L, 5L, 2L, 6L, 1L, 3L, 4L, 5L, 2L, 6L, 1L, 3L, 4L, 5L, 
2L, 6L, 1L, 3L, 4L, 5L, 2L, 6L, 1L, 3L, 4L, 5L, 2L, 6L, 1L, 3L, 
4L, 5L, 2L, 6L, 1L, 3L, 4L, 5L, 2L, 6L, 1L, 3L, 4L, 5L, 2L, 6L, 
1L, 3L, 4L, 5L, 2L, 6L, 1L), levels = c("true", "ignore", "BPA2012", 
"corrected(1)", "corrected(100)", "model_est"), class = "factor"), 
    cover_count = c(3243L, 4916L, 4918L, 4772L, 4825L, 4769L, 
    3266L, 4918L, 4919L, 4751L, 4810L, 4774L, 3341L, 4886L, 4886L, 
    4733L, 4821L, 4751L, 3134L, 4798L, 4794L, 4687L, 4816L, 4783L, 
    2030L, 4285L, 4279L, 4298L, 4828L, 4745L, 1859L, 4810L, 4809L, 
    4733L, 4805L, 4722L, 1896L, 4822L, 4820L, 4739L, 4817L, 4747L, 
    2041L, 4821L, 4825L, 4744L, 4803L, 4737L, 2284L, 4827L, 4826L, 
    4752L, 4815L, 4756L, 2760L, 4806L, 4808L, 4773L, 4835L, 4758L, 
    959L, 4740L, 4739L, 4719L, 4796L, 4721L, 976L, 4754L, 4761L, 
    4738L, 4805L, 4734L, 1101L, 4755L, 4759L, 4732L, 4793L, 4728L, 
    1202L, 4795L, 4796L, 4772L, 4820L, 4765L, 1610L, 4842L, 4840L, 
    4788L, 4835L, 4758L), cover = c(0.6486, 0.9832, 0.9836, 0.9544, 
    0.965, 0.9538, 0.6532, 0.9836, 0.9838, 0.9502, 0.962, 0.9548, 
    0.6682, 0.9772, 0.9772, 0.9466, 0.9642, 0.9502, 0.6268, 0.9596, 
    0.9588, 0.9374, 0.9632, 0.9566, 0.406, 0.857, 0.8558, 0.8596, 
    0.9656, 0.949, 0.3718, 0.962, 0.9618, 0.9466, 0.961, 0.9444, 
    0.3792, 0.9644, 0.964, 0.9478, 0.9634, 0.9494, 0.4082, 0.9642, 
    0.965, 0.9488, 0.9606, 0.9474, 0.4568, 0.9654, 0.9652, 0.9504, 
    0.963, 0.9512, 0.552, 0.9612, 0.9616, 0.9546, 0.967, 0.9516, 
    0.1918, 0.948, 0.9478, 0.9438, 0.9592, 0.9442, 0.1952, 0.9508, 
    0.9522, 0.9476, 0.961, 0.9468, 0.2202, 0.951, 0.9518, 0.9464, 
    0.9586, 0.9456, 0.2404, 0.959, 0.9592, 0.9544, 0.964, 0.953, 
    0.322, 0.9684, 0.968, 0.9576, 0.967, 0.9516)), row.names = c(NA, 
-90L), class = "data.frame")


library(ggplot2)

ggplot(cover, aes(x = r, y = cover)) + ylab("coverage (95% CI)") + 
	geom_hline(yintercept = 0.95) + 
	geom_point(color = "#00B937")

ggplot2 version 3.5.1 in R 4.4.1

@teunbrand
Copy link
Collaborator

teunbrand commented Feb 28, 2025

Thanks for the report! I cannot reproduce this on my end on Windows 11 R 4.4.2 and ggplot2 3.5.1. Opening the .wmf file in paint or affinity designer just shows the plot panel filling all the appropriate area. Using the 'copy as metafile' option in the RGui seems normal too.
What happens on your end if you repeat the procedure on the following graphic?

library(grid)
grid.newpage()
grid.draw(rectGrob(width = 0.9, height = 0.9))

@telenskyt
Copy link
Author

Thanks @teunbrand. Your code produces the same faulty behaviour on my setup.

It is however true that there might perhaps be some non-determinism in this. Because I observed the issue a month ago with different code, and not with this one, after I restarted the R session, now I observe it with this one.

@teunbrand
Copy link
Collaborator

Plots and the grid based rectangle rectangle above should dynamically adapt to fit the available size reported by the device during rendering. Based on #4541 (comment), you might try to set ggsave("cover.wmf", xpinch = 96, ypinch = 96). Other than this, I'm afraid it is more of a graphics device issue than a ggplot2 issue and mostly out of our hands.

@telenskyt
Copy link
Author

Thanks @teunbrand.

  1. The ggsave() workaround doesn't work for me.
  2. You are right that this bug is also present without ggplot2, e.g. with plot(1, 1)!

@teunbrand
Copy link
Collaborator

Thanks for confirming with plot()! In this case, I'll close this issue as there is nothing actionable that ggplot2 can do here.

@teunbrand teunbrand closed this as not planned Won't fix, can't repro, duplicate, stale Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants