【问题标题】:HTML graph in rmarkdown PDF size is too smallrmarkdown PDF 中的 HTML 图形太小
【发布时间】:2017-10-14 10:07:49
【问题描述】:

我正在尝试使用rmarkdown 在 PDF 输出中获取 HTML 图表:

---
title: "Test report"
output: pdf_document
always_allow_html: yes
params:
  plot: NA
---


```{r, echo=FALSE,message=FALSE, fig.height=4, fig.width=10, fig.show='hold'}
library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = ~carat, y = ~price, color = ~carat,
        size = ~carat, text = ~paste("Clarity: ", clarity))
```

作为 html 输出可以正常工作,但是当它是 pdf 格式时,图形非常小。任何想法如何解决这个问题?

【问题讨论】:

  • 为什么要在静态 pdf 文档中使用交互式绘图?对于任何静态绘图,我建议使用 base R 的绘图例程或 ggplot2
  • 客户想要一份 PDF 和一份 HTML 报告,它们需要看起来相似。我尝试了ggplot2,但无法复制图表。
  • 我明白了。但是plotly 确实适用于交互式文档。我通常使用ggplot 生成我的绘图,然后如果需要,通过将其包装在函数ggplotly 中来创建一个交互式版本。静态图和交互式图的格式将非常相似。
  • 我应该澄清一下,您可以使用plotly 在PDF 中生成静态图(参见例如here);但是,当采用ggplot + ggplotly 路线时,布局和格式可能更容易调整,并且可以确保 PDF 和 HTML 输出的绘图一致。
  • 感谢@MauritsEvers 的帮助。对我来说需要注意的是,我在文档中使用了 dygraphs、highcharter、plotly 和 html 地图。就在这个例子中,我提到了一个plotly 例子。

标签: html r pdf r-markdown plotly


【解决方案1】:

我遇到了同样的问题并通过在opts_chunk 中添加out.width = "100%" 解决了:

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = ">",
  out.width = "100%"
)

【讨论】:

    猜你喜欢
    • 2021-03-06
    • 2019-10-09
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2017-01-26
    • 2012-11-12
    • 1970-01-01
    相关资源
    最近更新 更多