5

On a R markdown file, does someone know why out.width,out.height,figure.width and figure.height parameters doesn't change plotly charts size when producing a pdf file? ( I precise that such parameters works perfectly using plot function)

Please find below a reproductible example with a Rmarkdown file

On this example, I would like the plotly chart to occupy the entire sheet like the plot chart.

---
title: "Change chart size chart on pdf file using plotly"
output:
  pdf_document: default
---

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

```

## Parameters doesn't work with plotly  

```{r, out.width='100%',out.height='100%',fig.height=20, fig.width=15, fig.align="left"}
library(plotly)
plot_ly(x = cars[1:10,]$speed,y = cars[1:10,]$dist)
```

## Parameters works using plot function

```{r,out.width='130%',out.height='100%', fig.height=20, fig.width=15, fig.align="left"}
plot(cars[1:10,])
```

enter image description here

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
JeanBertin
  • 633
  • 1
  • 7
  • 23
  • 1
    For those rendering into HTML via knitr (rather than PDF), the out.width and out.height chunk.options will work with plotly objects. Note that since plotly has no 'auto' or '%' width/height options, you will need to set the sizing requirements in the chunks if you want something like "100%". – dmanuge Mar 01 '19 at 20:06

3 Answers3

6

Plotly graphs are primarily designed for interactive outputs, and as such, can behave a bit strange when being exported to static images in PDF. This issue has had some similar posts in the past, and appears to come from how webshot creates the static image.

You can fix this by forcing the plotly graph dimensions when creating the graph. The plot_ly function has the arguments width and height which let you set the output dimensions of the resulting plot.

If you want to include HTML objects in PDF reports, you can use the webshot package which essentially takes a screenshot of the rendered plots and converts it into a static image for you to include in the report. This is explained well in the bookdown book. To install it, you will need to run:

install.packages('webshot')
webshot::install_phantomjs()

Once webshot is installed, it should work automatically:

---
title: "Change chart size chart on pdf file using plotly"
output:
  pdf_document: default
papersize: a4
---

```{r include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
library(plotly)
```

```{r, out.width="100%"}
plot_ly(x = cars[1:10,]$speed,y = cars[1:10,]$dist, width = 1000, height = 1200)
```

enter image description here

Will update this answer if I can work out exactly why it works, but hope that helps!

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • This MWE does not produce this output with the latest versions of R/RStudio/Plotly. – oliversm May 06 '20 at 12:05
  • Hi @oliversm, I have amended the post to make a slight tweak. It looked like it was rendering over two pages. Or was the issue something different? – Michael Harper May 07 '20 at 10:13
  • In my version (all updated yesterday) it requires, `always_allow_html: true`, and also the `type = 'scatter', mode = 'lines'` added. After all that when trying to knit to pdf no figure is shown. Trying to produce pdfs with plotly seems to have this well documented pitfall https://stackoverflow.com/questions/40268152/plotly-as-png-in-knitr-rmarkdown/40269138#40269138 – oliversm May 07 '20 at 11:33
  • Sounds like you might not have webshot installed? I have added to the answer to clarify as admittedly it was not that clear. – Michael Harper May 07 '20 at 15:51
  • That's fixed it for me. – oliversm May 14 '20 at 11:03
4

You need to be very careful while working with plotly graphs with r markdown pdf.

While creating the plotly graph,

  • Don't forget to explicitly set the size (width and height) of the graph
  • Use the chunk option of out.width and out.height. They both accept pt,mm,in,px,%
  • If you're producing the latex output in the pdf then 'px' will not work.

Please find the below code for the graph and the output of the same.

f <- list(
    size = 30,
    family = 'sans-serif'
  )
  m <- list(
    l = 100,
    r = 50,
    b = 0,
    t = 0,
    pad = 4
  )

p <- plot_ly(width = 800, height = 800) %>% 
  add_markers(data = pressure, x = pressure$temperature, y = pressure$pressure) %>% 
  layout(font = f, margin = m)
p

The output produced by this is with size and margins

Now after modifying the code chunk options as below:

```{r pressure2, echo=FALSE, out.height="150%", out.width="150%"}
f <- list(
    size = 30,
    family = 'sans-serif'
  )
  m <- list(
    l = 100,
    r = 50,
    b = 0,
    t = 0,
    pad = 4
  )

p <- plot_ly(width = 800, height = 800) %>% 
  add_markers(data = pressure, x = pressure$temperature, y = pressure$pressure) %>% 
  layout(font = f, margin = m)
p
```

you will get a much bigger graph

Keep coding!

Vishal Sharma
  • 289
  • 2
  • 10
1

Have you tried only using fig.height & fig.width options and remove out.width and out.height?

{r , fig.height=6, fig.width=9.5}

I am playing around with plotly and I've had success using HTML notebooks using this. It's a little more trial and error but you don't have to rebuild the plots.