【问题标题】:Center subcaption in Rmarkdown using knitr使用 knitr 在 Rmarkdown 中居中子标题
【发布时间】:2020-06-09 13:38:00
【问题描述】:

我在使用 Rmarkdown 的多个图表中使用子标题时遇到问题。我希望我的所有 4 个图形都对齐并居中,但是当我设置 fig.ncol = 1 时,只有最后一个图形居中,其他 3 个图形稍微偏右。

我已经尝试解决的问题:

  • 将 fig.ncol = 1 替换为 fig.sep = c('\newline', '\newline', '\newline'),但结果相同

这是我的代码:

---
 title: "Test"
 author: "My Name"
 output: pdf_document
 header-includes:
   - \usepackage{subfig}
---  


```{r, echo = FALSE, fig.cap = "Main Title", fig.subcap=c("Sub1","Sub2", "Sub3", "Sub4"), out.width='.30\\linewidth', out.width='.30\\linewidth', fig.asp=1, fig.ncol = 1, fig.align = c('center')}

library(ggplot2)

# line chart
 p1<- ggplot(pressure, aes(x = temperature, y = pressure)) +
         geom_line()

 # step chart
 p2<-ggplot(pressure, aes(x = temperature, y = pressure)) +
         geom_step()


 # line chart with points
 p3<-ggplot(pressure, aes(x = temperature, y = pressure)) +
         geom_line() +
         geom_point()

 # line chart
 p4<-ggplot(pressure, aes(x = temperature, y = pressure)) +
         geom_line()


 p1
 p2
 p3
 p4
 ``` 

输出如下所示:

任何想法如何让我的 4 个图表居中?提前致谢。

【问题讨论】:

    标签: r r-markdown markdown knitr tex


    【解决方案1】:

    不知道您使用的是哪个 .pdf 渲染器。我会做的是使用{patchwork} 将图排列在一个网格中。这样,您在渲染时将只有一个元素可以安排。

    主要区别在于您必须在创建绘图时指定绘图标题(随意更改字体)。然后,您必须在块选项中调整 fig.widthfig.height 才能正确渲染。

    library(ggplot2)
    library(patchwork)
    
    p3 <- ggplot(mtcars) +
        geom_smooth(aes(disp, qsec)) +
        labs(caption = "Some nice \n two line caption here") +
        theme(plot.caption.position = "plot",
              # Font family might differ depending on your machine
              plot.caption = element_text(hjust = 0.5, family = "Free Serif"))
    
    p4 <- ggplot(mtcars) + 
        geom_bar(aes(carb)) +
        labs(caption = "Another cool caption") +
        theme(plot.caption.position = "plot",
              plot.caption = element_text(hjust = 0.5, family = "Free Serif"))
    
    p3 / p4
    #> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
    

    reprex package (v0.3.0) 于 2020-06-09 创建

    【讨论】:

    • 也是一个不错的选择。将看看拼凑包。谢谢!
    【解决方案2】:

    尝试使用ggpubr 包中的ggarrange()。您可以从那里调整各种选项。

    ```{r, echo = FALSE, fig.cap = "Main Title", fig.asp=1}
    
    library(ggplot2)
    library(ggpubr)
    
    par(mfrow=c(4,1))
    # line chart
     p1<- ggplot(pressure, aes(x = temperature, y = pressure)) +
              geom_line() + 
              labs(caption='Sub1') + 
              theme(plot.caption = element_text(hjust = 0.5))
    
     # step chart
     p2<-ggplot(pressure, aes(x = temperature, y = pressure)) +
             geom_step() + 
              labs(caption='Sub2') + 
              theme(plot.caption = element_text(hjust = 0.5))
    
    
     # line chart with points
     p3<-ggplot(pressure, aes(x = temperature, y = pressure)) +
             geom_line() +
             geom_point() + 
              labs(caption='Sub3') + 
              theme(plot.caption = element_text(hjust = 0.5))
    
     # line chart
     p4<-ggplot(pressure, aes(x = temperature, y = pressure)) +
             geom_line() + 
              labs(caption='Sub4') + 
              theme(plot.caption = element_text(hjust = 0.5))
    
    ggarrange(p1, p2, p3, p4, nrow=4)
    

    【讨论】:

    • 谢谢,我会看看这个包似乎是一个不错的选择。
    猜你喜欢
    • 2021-02-06
    • 2018-03-12
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多