knitr: How to show two plots of different sizes next to each other?

前端 未结 4 659
萌比男神i
萌比男神i 2020-12-09 11:28

I wanted to generate two images of different sizes, but show them side-by-side. Is this possible?

This works, but then they have to be the same size:



        
相关标签:
4条回答
  • 2020-12-09 11:57

    Yet another option is to use a vector for out.width or out.height, if you do not mind resizing the plots, e.g.

    ```{r out.width=c('500px', '300px'), fig.show='hold'}
    boxplot(1:10)
    plot(rnorm(10))
    ```
    
    0 讨论(0)
  • 2020-12-09 12:02

    You can also use grid.arrange from gridExtra which works with grob or ggplot objects

    require(gridExtra)
    pre_fig <- rasterGrob(readPNG("paper_figures/surf_0000.png"), interpolate=TRUE)
    post_fig <- rasterGrob(readPNG("paper_figures/surf_0044.png"), interpolate=TRUE)
    grid.arrange(pre_fig, post_fig, ncol=2)
    
    0 讨论(0)
  • 2020-12-09 12:06

    One option would be to make a single wide graph with the R commands and give knitr just the one graph to deal with, maybe something like:

    ```{r fig.width=10, fig.height=9}
    layout( cbind( c(0,0,1,1,1,1,1,0,0), rep(2,9) ) )
    plot(...)
    plot(...)
    ```
    
    0 讨论(0)
  • 2020-12-09 12:16

    Another option, if you're outputting to HTML is to use the out.extra= chunk option, and set them to be float objects within a block. For example.

    ```{r fig.width=4, fig.height=6,echo=FALSE,out.extra='style="float:left"'}
    plot(cars)
    ```{r fig.width=8, fig.height=6,echo=FALSE, out.extra='style="float:left"'}
    plot(cars)
    ```
    
    0 讨论(0)
提交回复
热议问题