Skip to content

Plots are rendered differently on different monitors #7028

Closed

Description

@Mashin6

I originally reported the problem in RMarkdown project 4521 and this might be issue related to 1827

System details

RStudio Edition : Desktop
RStudio Version : 1.3.959
OS Version      : macOS 10.14.6
R Version       : 3.6.1

Steps to reproduce the problem

  1. In R Markdown document create a plot that has many labels
  2. Move R Studio window to different monitor
  3. Slightly resize R Studio window
  4. Rerun plot code

Describe the problem in detail

RStudio renders figures inconsistently depending on which monitor is the code executed. When I run code chunk on retina display the text in the figure is rendered smaller, but when I move the window to my second 2K display (ThinkVision 23.8'' P24H-10 QHD) all the labels and margins are rendered way larger, which dramatically change how the figure looks like.

Here is a sample code (cowplot package is to exaggerate the issue, but it behaves the same when cowplot is deactivated)

{r message=FALSE, warning=FALSE, fig.width = 8, fig.height = 6}
library(tidyverse)
library(cowplot)

data <- data.frame(x = rnorm(999), y = rep(c("a", "b", "c")))

p1 <- p2 <- data %>% ggplot() +
geom_histogram(aes(x = x, fill = y)) +
facet_wrap(vars(y)) +
scale_x_log10() +
labs(title = "Test subtitile")

plot_grid(p1, p1, labels = c("A", "B"), ncol = 1, label_size = 20) +
draw_plot_label("Test main titile", size = 25, x = 0.5, y = 1.015, hjust = 0.5, vjust = 0.5) +
theme(plot.margin = unit(c(1.5,0,0,0), "cm"))

Output 1:1 screenshots:

83458112-df993780-a42f-11ea-95df-9f015dfdd99f

Activity

  1. stale commented on Feb 5, 2021

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, per https://github.com/rstudio/rstudio/wiki/Issue-Grooming. Thank you for your contributions.

  2. added
    stale
    on Feb 5, 2021
  3. stale commented on Feb 19, 2021

    @stale

    This issue has been automatically closed due to inactivity.

  4. EoinTravers commented on Mar 9, 2021

    @EoinTravers

    I have the same issue.
    I can add the following:

    This isn't restricted to ggplot

    ```{r fig.width=4, fig.height=4}
    plot(iris$Sepal.Length, iris$Sepal.Width)
    ```
    

    Retina display:

    image

    External display:

    image

    HTML Output Matches External Display

    When the document is knit to html, using the default options, the generated images are small, the same as those seen on the external display.

    fig.retina

    Kniting with default settings, the knitr fig.retina parameter is set to 2.

    knitr::opts_chunk$get('fig.retina')
    ## [1] 2
    

    Changing this in the yaml

    ---
    title: "Figure Problems"
    output: 
      html_document:
        fig_retina: 1
    ---
    

    is registered...

    knitr::opts_chunk$get('fig.retina')
    ## [1] 1
    

    ...but doesn't change the size of the output figures, suggesting the issue might be somewhere in the knitr or rmarkdown packages.

    Changing the setting for a specific chunk also has no effect on the output.

    ```{r fig.width=4, fig.height=4, fig.retina=2}
    knitr::opts_chunk$get('fig.retina')
    plot(iris$Sepal.Length, iris$Sepal.Width)
    ```
    

    System Info

    R version 4.0.3 (2020-10-10)
    Platform: x86_64-apple-darwin17.0 (64-bit)
    Running under: macOS Big Sur 10.16
    RStudio: 1.4.1103 (desktop)
    
  5. cderv commented on Jan 10, 2022

    @cderv
    Collaborator

    I don't know if there is an issue with RStudio or not. Regarding last comment, I answered here rstudio/rmarkdown#1827 (comment) on how fig.retina plays out to me.

  6. EoinTravers commented on Jan 12, 2022

    @EoinTravers

    This discussion on twitter is relevant to this issue, and includes this nice but hacky fix.

  7. Mashin6 commented on Jan 12, 2022

    @Mashin6
    Author

    Referencing related issues #2098 and #7892. (thanks @cderv for looking those up)

  8. cderv commented on Jan 12, 2022

    @cderv
    Collaborator

    @ronblum would it be possible to reopen this issue closed by stale bot ? Seems still an issue and related to a few others about resolution of figure in Notebook . Thanks!

  9. removed
    stale
    on Jan 12, 2022
  10. jeffvroom commented on Feb 21, 2022

    @jeffvroom
    Contributor

    I see a few problems that mostly center around [this code] (

    width = width * pixelRatio,
    height = height * pixelRatio,
    units = units,
    res = 96 * pixelRatio
    ) where it sets up the device to render the graph to a PNG image.

    1. It hardcodes the dpi used to convert inches to pixels as 96 rather than looking at the dpi parameter (if one is supplied).
    2. It ignores fig.retina if it's being set in the annotations but instead always uses devicePixelRatio that comes from the IDE that gets it from the browser's window.devicePixelRatio. Here is where we get fig.width/height but do not get fig.retina to override the devicePixelRatio.
    3. It multiplies the width and height by devicePixelRatio as well as res and that doubles the effect. So in my test, with fig.width/fig.height = 2", the resulting resolution is 768 that would be 4" on a display with 96 dpi.

    In at least one customer situation, it seems like we need a way to disable the effect of the retina, or to at least not double it up because the results of ggplot are not consistent with other graphics they are rendering.

  11. 2 remaining items

  12. ronblum commented on Dec 2, 2022

    @ronblum
    Contributor

    Moving to Cherry Blossom, in order to verify that it's fixed by PR #12324. If it's not, we'll reassign it to the current milestone.

  13. jonvanausdeln commented on Feb 6, 2023

    @jonvanausdeln

    I tried on latest daily, 2023.03.0-daily+280. However, I would like confirmation from @Mashin6 and/or @EoinTravers that this solves their issue.

  14. Mashin6 commented on Feb 7, 2023

    @Mashin6
    Author

    I played a bit with the latest 2023.03.0-daily+293 and for me the main issue seems to be gone. Figures are rendered with the same proportion of text size, spacings, etc.
    However I noticed that at different screen resolutions/scalings the figures are rendered at different pixel dimensions. My suspicion is that the problem is somehow related to non-HiDPI resolutions/monitors.

    retina display - all resolutions - figure dimensions 768 × 768 pixels
    retina-1512x982

    External Monitor - 2560x1140 (native) - figure dimensions 384 × 384 pixels
    External-2560x1440

    External Monitor - 1280x720 (HiDPI scale) - figure dimensions 768 × 768 pixels
    External-1280x720

  15. jonvanausdeln commented on Feb 7, 2023

    @jonvanausdeln

    Thank you @Mashin6! Since the main issue is resolved, I will close out this issue. Would you be able to open a new issue on what you are still experiencing? I would be happy to open if you'd like, I just don't want to miss any details.

  16. frederikziebell commented on Jun 6, 2024

    @frederikziebell

    @jeffvroom Is there a way to set the device pixel ratio from a current RStudio session, similar to the .Call you described above to view it?

  17. kevinushey commented on Jun 6, 2024

    @kevinushey
    Collaborator

    @frederikziebell I suspect not, but you might want to file a separate issue to make sure your request isn't lost.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions