Skip to content

Plots in Windows are always heavily aliased #2142

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

Open
jackbrookes opened this issue Feb 6, 2018 · 27 comments · Fixed by #6520
Open

Plots in Windows are always heavily aliased #2142

jackbrookes opened this issue Feb 6, 2018 · 27 comments · Fixed by #6520

Comments

@jackbrookes
Copy link

jackbrookes commented Feb 6, 2018

Default plots in WIndows have terrible aliasing. This can be fixed by using type = "cairo" when saving with (e.g.) ggsave. However, the Plots pane in RStudio doesn't use this option. Often it is convenient to save plots directly from the editor but this leads to ugly plots, especially when using small points or thin lines.

ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + 
    geom_point() +
    stat_smooth(method = "lm", col = "red") + 
    labs(title = "Normal") +
    ggsave("normal.png") +
    labs(title = "Cairo") +
    ggsave("cairo.png", type = "cairo")

5f688f5b706e8f4397ee749afce6dd649f6cc2e9
(Notice, in particular, jagged red line when zoomed in on normal plot)

This could be solved by having an option for cairo rendering in RStudio or making cairo the default.

@ronblum
Copy link
Contributor

ronblum commented Feb 6, 2018

@jackbrookes I agree with you about the default on Windows, and how much better it looks with Cairo. We'll consider your enhancement request to add an option for type in future releases. Thank you!

@kevinushey
Copy link
Contributor

I'm putting this into the v1.3 milestone for consideration (this will become more important especially as high DPI displays become more common on Windows machines as well)

@kevinushey
Copy link
Contributor

kevinushey commented Feb 4, 2020

Here's a hacky way to force RStudio to make anti-aliased plots on Windows:

library(ggplot2)

trace(grDevices:::png, quote({
  if (missing(type) && missing(antialias)) {
    type <- "cairo-png"
    antialias <- "subpixel"
  }
}), print = FALSE)

ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + 
  geom_point() +
  stat_smooth(method = "lm", col = "red")

You can change type and antialias as appropriate / preferred for the plots generated by RStudio. This is at least a stop-gap solution until we get something properly implemented on the RStudio side. (Unfortunately I don't think we're going to fit the window for v1.3)

@kevinushey kevinushey modified the milestones: v1.3, v1.4 Feb 4, 2020
@GitDro
Copy link

GitDro commented Feb 7, 2020

Here's a hacky way to force RStudio to make anti-aliased plots on Windows:

library(ggplot2)

trace(grDevices:::png, quote({
  if (missing(type) && missing(antialias)) {
    type <- "cairo-png"
    antialias <- "subpixel"
  }
}), print = FALSE)

ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + 
  geom_point() +
  stat_smooth(method = "lm", col = "red")

You can change type and antialias as appropriate / preferred for the plots generated by RStudio. This is at least a stop-gap solution until we get something properly implemented on the RStudio side. (Unfortunately I don't think we're going to fit the window for v1.3)

Thanks for looking into it further. The fix works for me within a regular R script but not within a R Notebook (Rmd) file. Error: Tracing function "png" in package "grDevices (not-exported)"
[1] "png".

Running latest version of R and latest stable R Studio on Win 10. Current workaround is to render notebooks on a MacOS machine before sharing with others.

@jackbrookes
Copy link
Author

jackbrookes commented Feb 7, 2020

Thanks for looking into it further. The fix works for me within a regular R script but not within a R Notebook (Rmd) file. Error: Tracing function "png" in package "grDevices (not-exported)"
[1] "png".

Running latest version of R and latest stable R Studio on Win 10. Current workaround is to render notebooks on a MacOS machine before sharing with others.

For Rmd files adding this to your setup chunk should fix the issue:

knitr::opts_knit$set(dev.args = list(type = "cairo"))

@ishisht
Copy link

ishisht commented Feb 11, 2020

Here's a hacky way to force RStudio to make anti-aliased plots on Windows:

Thanks a lot! Works for me in the plot window, but not in the zoom window - does that use a different device?

@JimboMahoney
Copy link

Here's a hacky way to force RStudio to make anti-aliased plots on Windows:

library(ggplot2)

trace(grDevices:::png, quote({
  if (missing(type) && missing(antialias)) {
    type <- "cairo-png"
    antialias <- "subpixel"
  }
}), print = FALSE)

ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + 
  geom_point() +
  stat_smooth(method = "lm", col = "red")

You can change type and antialias as appropriate / preferred for the plots generated by RStudio.

Awesome, thanks! I've also been disappointed / frustrated at the sucky graphics on Windows vs. Mac (at least in the "live" plots in RStudio - I know they can be just as beautiful when saved as PNG etc.).

Could you (or someone) explain what this code does? It's brilliant and just what I've been looking for!

I'm guessing it takes the graphics as input and spits out an anti-aliased PNG in the plots panel? (I've found Cairo mentioned before, but not used the "missing" or "trace" functions.

@abuegho
Copy link

abuegho commented Apr 19, 2020

Here's a hacky way to force RStudio to make anti-aliased plots on Windows:

library(ggplot2)

trace(grDevices:::png, quote({
  if (missing(type) && missing(antialias)) {
    type <- "cairo-png"
    antialias <- "subpixel"
  }
}), print = FALSE)

ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) + 
  geom_point() +
  stat_smooth(method = "lm", col = "red")

You can change type and antialias as appropriate / preferred for the plots generated by RStudio. This is at least a stop-gap solution until we get something properly implemented on the RStudio side. (Unfortunately I don't think we're going to fit the window for v1.3)

Thank you, that's awesome! Any way to make this a default setting, instead of running the code every session?

@kevinushey
Copy link
Contributor

You could set it in your .Rprofile.

@yixuan
Copy link

yixuan commented May 2, 2020

@kevinushey This hack is so useful that it even solved a longstanding issue of showtext with RStudio: yixuan/showtext#31. I owe you a thousand thanks. 😉

@ishisht If it does not work for the zoom window, then modify the code as below:

trace(grDevices::png, quote({
  if (missing(type) && missing(antialias)) {
    type <- "cairo-png"
    antialias <- "subpixel"
  }
}), print = FALSE)

The tiny difference is that you now trace the exported function grDevices::png instead of the unexported one grDevices:::png. The reason is that the unexported version grDevices:::png() is called for the default window, but the exported version png() is called for the zoom window.

@R-Hannibal
Copy link

@jackbrookes , thanks again for finding and reporting this issue. We now have a fix in place in our latest 1.4 daily build.

If you get the chance will you let us know if the issue is resolved for you in that build?

@R-Hannibal R-Hannibal self-assigned this Aug 12, 2020
@jackbrookes
Copy link
Author

jackbrookes commented Aug 13, 2020

@R-Hannibal

@jackbrookes , thanks again for finding and reporting this issue. We now have a fix in place in our latest 1.4 daily build.

If you get the chance will you let us know if the issue is resolved for you in that build?

Unfortunately, the latest build did not change anything for me. Here is a screenshot of the Plot pane:

image

And even the saved file has the same issue:

normal

@kevinushey
Copy link
Contributor

Does it make a difference if you alter the graphics device settings (Tools - > Global Options... -> General -> Graphics)?

Screen Shot 2020-08-14 at 12 13 16 AM

@jackbrookes
Copy link
Author

jackbrookes commented Aug 14, 2020

Kind of, I can get good looking plot in the plots window if I set Backend to Cairo (not Cairo PNG), and Antialiasing to Gray or (Default). Other options don't work for me.

@melissa-barca
Copy link
Contributor

@kevinushey it seems like this is still an issue in some cases, should it be reopened?

@kevinushey kevinushey modified the milestones: v1.4, v1.5 Nov 20, 2020
@kevinushey kevinushey reopened this Nov 20, 2020
@kevinushey
Copy link
Contributor

I've re-opened the issue, but moved out of 1.4 as I don't think we can apply any further fixes in time for this release.

@isaactpetersen
Copy link

isaactpetersen commented Nov 21, 2020

When I use the following code in an R Notebook, my lines look better, but the fill of shapes/ribbons is still heavily aliased (and using three colons instead of two did not work):

trace(grDevices::png, quote({
  if (missing(type) && missing(antialias)) {
    type <- "cairo-png"
    antialias <- "subpixel"
  }
}), print = FALSE)

Here's are a few examples of output:
image

image

@jackbrookes
Copy link
Author

In RMarkdown documents, adding this to a setup block fixes it for me:

knitr::opts_knit$set(dev.args = list(type = "cairo"))

However this issue is around fixing this by default in Windows

@isaactpetersen
Copy link

isaactpetersen commented Nov 22, 2020

In RMarkdown documents, adding this to a setup block fixes it for me:

knitr::opts_knit$set(dev.args = list(type = "cairo"))

However this issue is around fixing this by default in Windows

As described here and here, the suggestion to use knitr::opts_knit$set(dev.args = list(type = "cairo")) works for RMarkdown documents, but it does not work with R Notebooks.

@Commifreak
Copy link

Hi,

as the changelog said: The default renderer used for the RStudio graphics device can now be customized. (#2142) - How to set this value globally in the server context for every user? What is the value of (Default) inside the option dialog?

Thanks!

@kevinushey
Copy link
Contributor

You can set the R option RStudioGD.backend; it should match one of the graphics backends here:

"enum": ["default", "cairo", "cairo-png", "quartz", "windows", "ragg"],

For example, to use ragg by default, you could set:

options(RStudioGD.backend = "ragg")

in (say) your R installation's Rprofile.site.

@kevinushey
Copy link
Contributor

Setting this in Rprofile.site might not work as expected currently (the user-preference will override anything set at startup). We should provide a mechanism for making this work a bit more cleanly.

@jmcphers
Copy link
Member

How to set this value globally in the server context for every user?

You can do this by creating a global user preference file, e.g., /etc/rstudio/rstudio-prefs.json (see https://docs.rstudio.com/ide/server-pro/r-sessions.html#user-preferences), with the following contents:

{
    graphics_backend: "ragg"
}

@Commifreak
Copy link

Commifreak commented May 6, 2021

@jmcphers Is this also possible for non-pro versions of the server?

EDIT: Yes, is is also working on non-pro servers ;)

@jmcphers
Copy link
Member

jmcphers commented May 6, 2021

Yes, it does work for both!

@gusberinger
Copy link

What ended up working for me was this command:

options(RStudioGD.backend = "cairo-png")

@dhml
Copy link

dhml commented Aug 7, 2024

{
    graphics_backend: "ragg"
}

Just noting that you'll likely get a JSON parsing error with the above so it needs to be valid JSON, e.g.:

{
  "graphics_backend": "cairo"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.