代码之家  ›  专栏  ›  技术社区  ›  JeanBertin

在R标记PDF输出中更改绘图图表大小的输出宽度

  •  3
  • JeanBertin  · 技术社区  · 7 年前

    有人知道为什么吗 out.width , out.height figure.width figure.height plot (功能)

    在本例中,我希望plotly图表像plotchart一样占据整个工作表。

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

    1 回复  |  直到 7 年前
        1
  •  6
  •   Michael Harper    5 年前

    Plotly图形主要是为交互式输出而设计的,因此,当以PDF格式导出为静态图像时,其行为可能有点奇怪。这个问题已经有了一些进展 similar posts in the past ,似乎来自webshot创建静态图像的方式。

    可以通过在创建图形时强制使用plotly graph尺寸来解决此问题。这个 plot_ly 函数具有参数 width height 可以设置结果图的输出尺寸。

    软件包,该软件包从本质上获取渲染图的屏幕截图,并将其转换为静态图像,供您包含在报告中。这在本书中得到了很好的解释 bookdown book

    install.packages('webshot')
    webshot::install_phantomjs()
    

    韦伯肖特

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

    将更新这个答案,如果我能弄清楚它究竟为什么工作,但希望这有帮助!

        2
  •  3
  •   Vishal Sharma    5 年前

    在使用r markdown pdf打印图形时,需要非常小心。

    创建绘图时,

    • 使用out.width和out.height的chunk选项。它们都接受pt,mm,in,px,%

    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
    

    由此产生的输出是 with size and margins

    现在修改代码块选项如下:

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

    你会得到一个 much bigger graph

    继续编码!