Skip to content

Figure size in RStudio is different than in rendered output #4521

Closed

Description

@slowkow

System details

RStudio Edition : Desktop
RStudio Version : 1.3.74
OS Version      : macOS 10.13.6
R Version       : 3.5.1

Steps to reproduce the problem

Create a new .Rmd file with this content:

---
title: "Untitled"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r mtcars, fig.width = 2, fig.height = 1}
library(ggplot2)

ggplot(mtcars, aes(wt, mpg)) +
  geom_point()
```

Run the chunk to see the output inside RStudio.

Knit the document to see the output in the rendered HTML.

Describe the problem in detail

The size of the figure in the RStudio interface is different than the size in the rendered output.

I spent time trying to adjust the fig.width and fig.height so the figure looks good when I run the chunk in RStudio.

It turns out my time is wasted because the rendered output does not use the same figure size as the figured in RStudio (see screenshot below).

Questions

  • How are users supposed to size their figures properly?

  • Are we meant to re-knit the file each time we tweak the fig.width and fig.height parameters?

    • If so, notice that the user must ignore the output as shown in RStudio while tweaking chunk options, since they will never share RStudio output with others and it is not representative of the knitted output.

image

Activity

  1. david-cortes commented on Mar 27, 2019

    @david-cortes

    The size of the plots in RStudio will also change if you resize the panel, if you click zoom, etc. How else would you expect it to work? you're not setting a pre-defined size in terms of pixels for your plot in there.

  2. slowkow commented on Mar 27, 2019

    @slowkow
    Author

    @david-cortes No, it does not change when I resize the panel. See the gif below.

    ezgif com-optimize (6)

  3. slowkow commented on Mar 27, 2019

    @slowkow
    Author

    RStudio ignores dpi, but respects fig.width and fig.height

    It seems that RStudio ignores the dpi option, but it respects the fig.width and fig.height options. This is not expected behavior.

    If RStudio respects fig.width and fig.height, then I also expect it to respect dpi.

    image

    The two output devices are different

    After trying a few more examples, I cannot get the output in the RStudio document to look the same as the output in the knitted document.

    I guess the graphics devices and their settings are different for the two outputs. I would have expected them to be more similar.

    This leaves the user in an undesirable situation:

    • The user cannot predict the appearance of the rendered output without knitting the document.

    • RStudio misleads the user to think the knitted document will look like the RStudio document. Then the user will be surprised when the output does not match what they saw while writing code and tweaking the chunk options.

    • Confusingly, the user must remember that a poor output in the RStudio document is actually desirable. This is because some chunk options are ignored in the RStudio interface (e.g. dpi), and because the user certainly cares more about the knitted document that will be seen by others than the RStudio document that will never be seen by others.

  4. ronblum commented on Mar 27, 2019

    @ronblum
    Contributor

    @slowkow Thank you for reporting this, especially describing your experience in such detail. As you know, RStudio 1.3 is in early stages of development and is meant for testing purposes only, but thank you for trying it out and letting us know issues that you run into!

  5. added this to the v1.3 milestone on Mar 27, 2019
  6. ronblum commented on Jun 7, 2019

    @ronblum
    Contributor

    See #4913 for a detailed example of the same behavior.

    Note: this is the behavior in 1.1 and 1.2, so it's not new.

  7. removed this from the v1.3 milestone on Jun 7, 2019
  8. alyamahmoud commented on Mar 22, 2020

    @alyamahmoud

    when passing fig.asp as in {r chunk_name, echo=TRUE, fig.asp = 1, fig.height=x, fig.width=y}, the issue was solved.

  9. charlesli37 commented on Jan 4, 2021

    @charlesli37

    when passing fig.asp as in {r chunk_name, echo=TRUE, fig.asp = 1, fig.height=x, fig.width=y}, the issue was solved.

    worked for me as well, after trying different values for fig.asp and fig.width

    fig.asp: (NULL; numeric) The aspect ratio of the plot, i.e., the ratio of height/width. When fig.asp is specified, the height of a plot (the chunk option fig.height) is calculated from fig.width * fig.asp.

    source

  10. mikebessuille commented on Feb 12, 2021

    @mikebessuille
    Contributor

    Workaround seems good, so closing this.

  11. 51 remaining items

  12. nikita-telkar commented on Mar 20, 2023

    @nikita-telkar

    Adding to this:

    Note: this has statrted happening after I've started using .qmd. This does not happen when I use a .Rmd. Figure dimenstions remain the same from .Rmd --> HTML.

    • fig.width = 10, fig.height = 5
    • Left: HTML; right: inline in RStusio IDE in Windows (2022.12.0 Build 353)

    I've spent hours trying to resize all of my graphs, thinking of switching back to .Rmd.

    image

  13. netique commented on May 10, 2023

    @netique
    Contributor

    @nikita-telkar Have you tried any recent daily releases? I think this should be fixed even in the latest stable. Could you take a try?

  14. modified the milestone: Product Backlog on Sep 20, 2023
  15. andrewheiss commented on Sep 26, 2023

    @andrewheiss

    Revisiting this really quick. Everything works great and as expected when using inline chunk options like fig.width and fig.height, but RStudio fails to recognize the dimensions when using YAML-style chunk options:

    ```{r fig.width=7, fig.height=2}
    # Correct dimensions
    plot(cars)
    ```
    
    ```{r}
    #| fig-width: 7
    #| fig-height: 2
    # Dimensions ignored
    plot(cars)
    ```
    image
  16. netique commented on Sep 27, 2023

    @netique
    Contributor

    @andrewheiss That's already tracked in #12305

  17. jgutman commented on Oct 16, 2023

    @jgutman
    Contributor

    @andrewheiss @netique is this issue still relevant then? From reviewing the history and linked issues it seems like this may have been solved by the fixes for #11708 (duplicated by #12305) and #12350/#12324

  18. andrewheiss commented on Oct 16, 2023

    @andrewheiss

    Oh cool, yeah, this issue isn't relevant—the resolution issues were fixed earlier, and #11708 addresses the YAML-style dimensions

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions