How to write complex kable and ggplot in sections

2019-08-16 02:08发布

I am trying to knit several kable and ggplot in sections in one page, details are in the minimal and reproducible example below.

I have tried

  • cat("\\twocolumn")
  • kable_styling(position = "float_right")
  • \usepackage{multicol} (Previous Question)

Below is the code

---
title: "Untitled"
output: pdf_document
classoption: landscape
---

\newpage

```{r cars, echo=FALSE, results="asis", fig.width=3, fig.height=2, message=FALSE}
library(dplyr)
library(knitr)
suppressWarnings(library(kableExtra))
library(ggplot2)

dt <- split(mtcars, f = mtcars[, "cyl"]) %>% 
  lapply(., function(x) x[1:5, 1:4])

for (i in seq_along(dt)) {
  print(
    kable(cbind(dt[[i]], dt[[i]], dt[[i]], dt[[i]], dt[[i]])) %>% 
      kable_styling("striped")
  )


  print(
    ggplot(data = dt[[i]], aes(x = mpg, y = cyl, group = 1)) + 
      geom_line(aes(y = disp), linetype = "solid", colour = "#000000")
  )


  print(
    ggplot(data = dt[[i]], aes(x = mpg, y = cyl, group = 1)) + 
      geom_line(aes(y = disp), linetype = "solid", colour = "#000000")
  )

  print(
    kable(dt[[i]]) %>% 
      kable_styling("striped") %>% 
      add_header_above(c(" " = 1, 
                         "Group 1" = 2, 
                         "Group 2" = 2))
  )

  cat("\\twocolumn")

  print(
    kable(dt[[i]]) %>% 
      kable_styling("striped") %>% 
      add_header_above(c(" " = 1, 
                         "Group 1" = 2, 
                         "Group 2" = 2))
  )

  cat("\\pagebreak")


  print(
    ggplot(data = dt[[i]], aes(x = mpg, y = cyl, group = 1)) + 
      geom_line(aes(y = disp), linetype = "solid", colour = "#000000")
  )
  cat("\\onecolumn")

  cat("\\pagebreak")
}
```

I want the final result to be:

  • each page, except the title page, contains
    • 1st row: kable
    • 2nd row: ggplot, ggplot, kable
    • 3rd row: kable, ggplot
  • section property
    • adjustable row height
    • adjustable column width

0条回答
登录 后发表回答