Quarto & (plotly.R | ggplotly): setting plot sizes for interactive & html

I’m trying to adjust plotly (& ggplotly) plots in a quarto doc. I want the plot to “look good” both for the interactive plotting (running each chunk) and when rendering the doc as html. There are at least 4 different ways of altering the plot size:

  • rmarkdown chunk options: {r, fig.width=4, fig.height=3}
  • quarto render options: #| fig-width: & #| fig-height:
  • ggplotly: ggplotly(width=, height=)
  • plotly layout: ggplotly() %>% layout(width=, height=)

Which should I use so that the figure has the “optimal” size for both interactive running of code chunks and html rendering? Why is width/height for layout() in px, while rmarkdown chunk width/height is set in inches?!?

Moreover, if I use ggplotly() %>% layout(width=, height=) with a somewhat large width, only some of the plot is visualized when interactively running code chunks. This occurs regardless of what I set for {r, fig.width=, fig.height=}.

How does one avoid spending all day just fiddling with plot sizes when using R, quarto, & plotly?

Hi @nyoungb2,

Welcome to the community! :slightly_smiling_face:

If I remember correctly, plotly on Rmd will only use out chunk options and not fig. Besides, it might be a better idea not to set absolute values for sizes in html, and instead use something like out.width="100%". If you really want to set the values manually, I would expect the last option (and maybe the 3rd) on the list to work, but not the others…

For your second question, everything in Plotly is made for html where the default is px. Rmarkdown was initially made for static documents, where defaults are in inches (like LaTeX).

Lastly, most of our community is using exclusively the python version of plotly (including me), so I would encourage you to ask your question in R forums or SO as well.

Hope this helps! :smiley:

Thanks @jlfsjunior !

I’ll see if out.width="100%" (or similar) works. In general, it doesn’t seem like there is a lot of (good) documentation on really setting figure sizes for both interactive running of code chunks and rendering html .

Do you know if I can change the defaults for rmarkdown width/height or (gg)plotly width/height? I didn’t see any docs in that regard. I’d like to use just one unit (inches or px).

Thanks for the advice on posting to R forums!

I don’t think it is possible, but I believe you can specify Rmd sizes in px. I reckon my wording was bad, as I meant standard where I said default.