Poor resolution in knitr using Rmd

后端 未结 3 1069
你的背包
你的背包 2020-12-29 02:30

I have a .Rmd file and I am trying to create a .docx file via the function pandoc.

I want to have a figure with final resolution of 504x504 pixels (i.e., 7x7inch wi

相关标签:
3条回答
  • 2020-12-29 02:59

    This is a great time to take advantage of knitr's built-in dynamic customization features for output types. Ths was tested with both output targets...

    ````{r img-setup, include=FALSE, cache=FALSE}
    out.format <- knitr::opts_knit$get("out.format")
    img_template <- switch( out.format,
                         word = list("img-params"=list(fig.width=6,
                                                       fig.height=6,
                                                       dpi=150)),
                         {
                           # default
                           list("img-params"=list( dpi=150,
                                                   fig.width=6,
                                                   fig.height=6,
                                                   out.width="504px",
                                                   out.height="504px"))
                         } )
    
    knitr::opts_template$set( img_template )
    ````
    

    If you don't want to use the img_template for every image produced you can either not call the set function and instead add opts.label="img_template" to the params of the chunks you want to use it with, or override the img_template by specifying the params explicitly for the chunk.

    0 讨论(0)
  • 2020-12-29 03:05

    Just keep things simple, set all chucks to dpi of 300 and make them wider.

    Run this first thing:

    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(dpi=300,fig.width=7)
    ```
    
    0 讨论(0)
  • 2020-12-29 03:21

    It's most likely that since this question was asked, the software has improved. I came to this question looking for how to increase the resolution of plots. I found OP's original approach worked out-of-the-box for me.

    So, setting dpi=300 (because dpi=150 did not produce a sufficiently obvious difference) in the chunk's parameters, produced a much higher quality image without modifying the physical size of the images within Word.

    ```{r, echo=FALSE, dpi=300, fig.width=7, fig.height=7}
    plot(0,0,type="n",xlim=c(0,500), ylim=c(-12,0), las=1)
    color  <-  rainbow(500)
    text(380,-1,"Test",pos=4)
    lseq   <-  seq(-6,-2,length.out=500)
    for(j in seq_along(lseq)) {
        lines(c(400,450), rep(lseq[j], 2), col=color[j])
    }
    polygon(c(400,450,450,400), c(-6,-6,-2,-2), lwd=1.2)
    ```
    

    However, setting out.width and out.height removes the production of the image entirely, with the warning "fig.align, out.width, out.height, out.extra are not supported for Word output".

    0 讨论(0)
提交回复
热议问题