Change output width of plotly chart size in R Markdown PDF output

10,135

Solution 1

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!

Solution 2

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!

Share:
10,135
JeanBertin
Author by

JeanBertin

Data Scientist at Veolia Water

Updated on June 05, 2022

Comments

  • JeanBertin
    JeanBertin about 2 years

    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

  • oliversm
    oliversm about 4 years
    This MWE does not produce this output with the latest versions of R/RStudio/Plotly.
  • Michael Harper
    Michael Harper about 4 years
    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?
  • oliversm
    oliversm about 4 years
    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 stackoverflow.com/questions/40268152/…
  • Michael Harper
    Michael Harper about 4 years
    Sounds like you might not have webshot installed? I have added to the answer to clarify as admittedly it was not that clear.
  • oliversm
    oliversm about 4 years
    That's fixed it for me.