tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.48k stars 2.02k forks source link

Problem with `geom_ribbon` and alpha combined with `coord-cartesian` in R 4.1.0 on Windows #4498

Closed NikNakk closed 2 years ago

NikNakk commented 3 years ago

Since upgrading to R 4.1.0, I've run into an issue with ggplot2 on Windows where setting an xlim using coord_cartesian can result in a geom_ribbon being suppressed altogether. I've been able to reproduce this on two different Windows 10 machines running R 4.1.0, both inside RStudio and RGui. Running the code below without the coord_cartesian works fine on all machines I've tested. With the coord_cartesian, it produces a plot with no ribbon on my two Windows 10 machines using R 4.1.0, but works fine on R 4.0.5 on Windows 10 as well as R 4.1.0 tested using Ubuntu 20.04. Note removing the alpha parameter makes the issue go away even on R 4.1.0 on Windows 10.

I first ran into this using the survminer package, but I've managed to get a reprex now which occurs just in ggplot2.

library(ggplot2)
ggplot(data.frame(x = 1:10, ymin = 1:10, ymax = 10 + 1:10), aes(x)) +
  geom_ribbon(aes(ymin = ymin, ymax = ymax), alpha = 0.5) +
  coord_cartesian(c(1, 9))

Created on 2021-06-01 by the reprex package (v2.0.0)

yutannihilation commented 3 years ago

Thanks for reporting, confirmed on my Windows. A quick workaround is to use AGG as the graphic backend of RStudio, but this sounds a bit serious in that this happens not only on RStudio, but also on RGui and even with ggsave().

clauswilke commented 3 years ago

Isn't this just again the problem that the graphics device on Windows can't clip properly?

yutannihilation commented 3 years ago

Hmm..., I thought it was the problem only with RStudio's Plot pane, but my memory might be wrong (I haven't been used Windows for years), so let me check...

clauswilke commented 3 years ago

Like e.g. #4029.

yutannihilation commented 3 years ago

Ah, true. Sorry, I was a bit confused.

@NikNakk

works fine on R 4.0.5 on Windows 10

Sorry, this seems a known issue. My guess is this is because you used AGG as the backend on RStudio with R 4.0.5, and after migrating to R 4.1.0 you don't install it yet?

NikNakk commented 3 years ago

No, it works fine on vanilla R 4.0.5 and RStudio (i.e. using the default graphics device for RStudio). On R 4.1.0 it doesn't work using any graphics device I've tried. I've just reproduced it using RGui.exe and using the png device. Identical code run on R 4.0.5 and R 4.1.0 (with no RStudio running) gives the following.

As mentioned, I've reproduced this on two different machines (both Win 10 x64). Both have RStudio installed, but it was not running for this, and the graphics device for png shouldn't depend on RStudio as far as I understand it. I presume this relates in the change to the graphics model for R 4.1.0.

R 4.0.5:

r-4 0 5

R 4.1.0:

r-4 1 0

Code used in both cases:

png("output.png")
library(ggplot2)
ggplot(data.frame(x = 1:10, ymin = 1:10, ymax = 10 + 1:10), aes(x)) +
  geom_ribbon(aes(ymin = ymin, ymax = ymax), alpha = 0.5) +
  coord_cartesian(c(1, 9))
dev.off()
NikNakk commented 3 years ago

I've also put together a very simple R package to reproduce this issue. Artefacts from Appveyor are available at https://ci.appveyor.com/project/NikNakk/ggplot2missingribbon/build/artifacts

Package itself is at https://github.com/NikNakk/ggplot2missingribbon

Note all it does is to generate a test PNG. On Appveyor, R 4.0.5 produces the correct output (and so 'fails' the check, because the snapshot is based on R 4.1.0), whereas R 4.1.0 produces an incorrect PNG, as does r-devel.

yutannihilation commented 3 years ago

Thanks for the detailed testing! I also confirmed this with my Windows with R 4.0.4 (sorry for my laziness...).

It seems this is a version of the known issue #4029, something related to alpha and clipping. If so, there's nothing we can do on ggplot2's side. Please use AGG as the graphic backend of RStudio.

I hope we can reproduce this with grid so that we can file a bug report to the R bugzilla.

NikNakk commented 3 years ago

Ok, fair enough, though it's odd that it reproducibly works fine for me on R 4.0.5 and not on R 4.1.0 across both my own machines and appveyor. For my personal use, I'll switch to ragg/AGG as suggested.

yutannihilation commented 3 years ago

Yeah, it's a bit worrisome that the situation seems to get worse in R 4.1.0...

clauswilke commented 3 years ago

Maybe we should copy @pmur002. Though we would probably need a reproducible example using only grid code to make further progress from here.

FBartos commented 3 years ago

Hi, encountered the same issue. Interestingly, it also happens when using ggplot2::alpha() to fill base plot polygons for example.

Switching to ragg in Rstudio fixed the issue for both base and ggplot

bergsmat commented 3 years ago

I have this problem too.

sessionInfo() R Under development (unstable) (2021-06-07 r80459) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363)

tamas-ferenci commented 3 years ago

As a sidenote, for a workaround for shiny you'll need this.

JWiley commented 3 years ago

I am getting this as well. Interestingly there seems to be a very small amount of "tolerance" for the lower limit of xlim as in I can clip a bit of the ribbon on the left hand side but not right hand side of the plot (maybe margin related?) This is on:

R version 4.1.0 (2021-05-18) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19043)

using Emacs so the default graphics device. pdf() works fine.

dandradem commented 2 years ago

Thanks for the reporting! I was starting to think that there was a problem with my code.

twest820 commented 2 years ago

I just hit this too. RStudio 2021.09.0, R 4.1.1, ggplot 3.3.5, Windows 10 21H1. To expand slightly on what's been reported, at least on the machine I'm using at the moment alpha = 1 can be passed explicitly and ribbons will still be drawn. It's only alpha < 1 which causes ribbons to disappear (any value in (0...1) as far as I can tell). I have options to set RStudio's graphics backend to (Default), Windows, Cairo, Cairo PNG, and AGG (Tools -> Global Options -> General -> Graphics) and disappearance of transparent ribbons occurs only with Windows (which appears to be the default). From a bit of searching, I'm guessing discussion above may have default to AGG on a performance basis.

This can be a somewhat impactful regression as AGG and Cairo render plots differently from Windows (and differently from each other), meaning themes may need to be modified for changes in things like default axis line widths and individual plots then updated for differences in how size affects point sizes and line widths, the amount of transparency provided by a given value of alpha, default choices of axis breaks, and probably other things I haven't noticed yet. Displayed fonts change in RStudio (altering sizes of plots, axes, legends, and so on, similar to what this blog post from last year found) but in the quick checks I've done so far it doesn't seem switching to AGG introduces new font changes in ggsave() to .png, though shifts in the meaning of alpha and size do flow through to the rendered files.

It's interesting, and probably a useful exercise, to expose implicit assumptions made about device behavior in setting up figures.

yutannihilation commented 2 years ago

If you want an workaround, why can't you just use AGG? Are there any reason to explore another option?

https://github.com/tidyverse/ggplot2/issues/4498#issuecomment-857707480

twest820 commented 2 years ago

Thanks, @yutannihilation! I'd read the first part of the thread but missed your update from June 10 when skimming the rest. Post above revised to more relevant information.

thomasp85 commented 2 years ago

Closing - the workaround is to not use the substandard windows device. There is nothing ggplot2 can do about this

yutannihilation commented 2 years ago

(For future reference for those who come here by accident)

To be clear, we've kept this open for a while only in the hope that someone might volunteer to file a ticket on R's Bugzilla with a reproducible example made with pure-grid code. As Thomas says, there is nothing ggplot2 can do about this.

pmur002 commented 2 years ago

My timing is out - I have just committed a fix (to semitransparent drawing on Windows) to r-devel (r81114) that I think fixes this.

yutannihilation commented 2 years ago

Wow, thanks so much for fixing! 🎉