简体   繁体   中英

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

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,])
```

在此处输入图像描述

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)
```

在此处输入图片说明

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

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM