[英]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?在 R markdown 文件中,有人知道为什么在生成 pdf 文件时out.width
、 out.height
、 figure.width
和figure.height
参数不会更改 plotly 图表大小吗? ( I precise that such parameters works perfectly using plot
function) (我精确地说,这些参数使用plot
函数可以完美工作)
Please find below a reproductible example with a Rmarkdown file请在下面找到带有 Rmarkdown 文件的可重现示例
On this example, I would like the plotly chart to occupy the entire sheet like the plot chart.在这个例子中,我希望 plotly 图表像 plot 图表一样占据整个工作表。
---
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.绘图图形主要是为交互式输出设计的,因此,在导出为 PDF 中的静态图像时,可能会表现得有点奇怪。 This issue has had some similar posts in the past , and appears to come from how webshot creates the static image.这个问题过去有一些类似的帖子,似乎来自 webshot 如何创建静态图像。
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. plot_ly
函数具有参数width
和height
,可让您设置结果图的输出尺寸。
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.如果您想在 PDF 报告中包含 HTML 对象,您可以使用webshot包,该包本质上截取渲染图的屏幕截图并将其转换为静态图像以供您包含在报告中。 This is explained well in the bookdown book .这在bookdown book 中有很好的解释。 To install it, you will need to run:要安装它,您需要运行:
install.packages('webshot')
webshot::install_phantomjs()
Once webshot is installed, it should work automatically:一旦安装了webshot ,它应该会自动运行:
---
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.使用 r markdown pdf 处理绘图时需要非常小心。
While creating the plotly graph,在创建情节图时,
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
?您是否尝试过仅使用fig.height
和fig.width
选项并删除out.width
和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.我正在玩 plotly,我已经成功地使用了 HTML 笔记本。 It's a little more trial and error but you don't have to rebuild the plots.这是一个多一点的试验和错误,但你不必重建地块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.