![](/img/trans.png)
[英]change font type and size of headings in R markdown with PDF output
[英]Change output width of plotly chart size in R Markdown PDF output
在 R markdown 文件中,有人知道为什么在生成 pdf 文件时out.width
、 out.height
、 figure.width
和figure.height
参数不会更改 plotly 图表大小吗? (我精确地说,这些参数使用plot
函数可以完美工作)
请在下面找到带有 Rmarkdown 文件的可重现示例
在这个例子中,我希望 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,])
```
绘图图形主要是为交互式输出设计的,因此,在导出为 PDF 中的静态图像时,可能会表现得有点奇怪。 这个问题过去有一些类似的帖子,似乎来自 webshot 如何创建静态图像。
您可以通过在创建图形时强制绘制图形尺寸来解决此问题。 plot_ly
函数具有参数width
和height
,可让您设置结果图的输出尺寸。
如果您想在 PDF 报告中包含 HTML 对象,您可以使用webshot包,该包本质上截取渲染图的屏幕截图并将其转换为静态图像以供您包含在报告中。 这在bookdown book 中有很好的解释。 要安装它,您需要运行:
install.packages('webshot')
webshot::install_phantomjs()
一旦安装了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)
```
如果我能弄清楚它为什么起作用,我会更新这个答案,但希望有帮助!
使用 r markdown pdf 处理绘图时需要非常小心。
在创建情节图时,
请找到下面的图形代码和相同的输出。
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
由此产生的输出具有大小和边距
现在修改代码块选项后如下:
```{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
```
你会得到一个更大的图表
继续编码!
您是否尝试过仅使用fig.height
和fig.width
选项并删除out.width
和out.height
?
{r, fig.height=6, fig.width=9.5}
我正在玩 plotly,我已经成功地使用了 HTML 笔记本。 这是一个多一点的试验和错误,但你不必重建地块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.