简体   繁体   English

R markdown:减少 pdf Z78E6221F6393D1356681DB398F 文档中两个图之间的空间

[英]R markdown: Reduce the space between two plots in pdf output document

  • Aim: R markdown: To construct one DinA4 pdf page with a rectangle on the top left side and two plots.目标:R markdown:构建一个 DinA4 pdf 页面,左上角有一个矩形和两个绘图。
  • Problem: After drawing the rectangle, the next plot is far away with a large white space in between.问题:绘制矩形后,下一个 plot 距离很远,中间有很大的空白。
  • Desired Output: Heatmap should appear immediately after the rectangle may with one or two white lines.所需的 Output:热图应立即出现在矩形之后,可能带有一两条白线。

I guess the problem is the drawing of the rectangle.我想问题是矩形的绘制。 Here I need some help.在这里我需要一些帮助。 Thank you.谢谢你。

---
output:
  pdf_document
documentclass: article
classoption: a4paper
geometry: margin=1cm

subparagraph: yes
header-includes: |
  \usepackage{titlesec}
  \titlespacing{\title}{0pt}{\parskip}{-\parskip}

title: "Example of Title to Body Text"
subtitle: Subtitle Places Here
---

\vspace{-5truemm}
\pagenumbering{gobble}
#``` {r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(draw)
library(ggplot2)
library(dplyr)
# ```
#```{r rectangle}
drawBox(x =2, y = 3.5, width = 2.5, height = 1)
#```
#```{r heatmap}
df <- data.frame(
  test_id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
              3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4),
  test_nr = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 
              1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 2, 2),
  region = c("A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", 
             "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", 
             "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D"),
  test_value = c(3, 1, 2, 2, 2, 1, 2, 2, 3, 2, 2, 3, 2, 1, 2, 2, 1, 2, 3, 
                 4, 2, 1, 1, 2, 1, 1, 1, 1, 2, 2, 3, 2, 2, 2, 99, 99, 3, 3, 3, 3)
)

# named vector for heatmap
cols <-  c("1" = "green", 
           "2" = "darkgreen", 
           "3" = "orange", 
           "4" = "red",
           "99" = "black")
labels_legend <- c("1" = "very good", 
                   "2" = "good", 
                   "3" = "not so good", 
                   "4" = "bad", 
                   "99" = "NA")

df <- df %>% 
  filter(test_id==1)

ggplot(
  df, 
  aes(region, test_nr)) +
  geom_tile(aes(fill= factor (test_value))) +
  geom_text(aes(label = test_value), size = 10, color = "white") + # text in tiles
  scale_colour_manual(
    values = cols, 
    breaks = c("1", "2", "3", "4", "99"),
    labels = labels_legend,
    aesthetics = c("colour", "fill")
  ) +
  theme(text = element_text(size = 14)) + # this will change all text size
  labs(title =  "Test (Individual heatmap)", x = "Region", y = "Event") +
  labs(fill = "Test") +
  coord_fixed(ratio=1, clip="on") +
  theme(axis.text.y = element_text(face = "bold", size = 12)) +
  theme(axis.text.x = element_text(angle = 0, face = "bold", size = 12)) +
  theme(axis.line = element_line(colour = "darkblue", 
                                 size = 1, linetype = "solid")
  )
# ```

## Information

You can use the subfigure environment to display multiple plots side by side, though you may not want to place the rectangle under the same main caption as the heatmap.您可以使用 subfigure 环境并排显示多个绘图,但您可能不希望将矩形放置在与热图相同的主标题下。

在此处输入图像描述

---
output:
  pdf_document:
    extra_dependencies: "subfig"
documentclass: article
classoption: a4paper
geometry: margin=1cm

subparagraph: yes
header-includes: |
  \usepackage{titlesec}
  \titlespacing{\title}{0pt}{\parskip}{-\parskip}

title: "Example of Title to Body Text"
subtitle: Subtitle Places Here
---

\vspace{-5truemm}
\pagenumbering{gobble}

``` {r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(draw)
library(ggplot2)
library(dplyr)
```

```{r rectangle}
drawBox(x =2, y = 3.5, width = 2.5, height = 1)
```

```{r heatmap-data}
df <- data.frame(
  test_id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
              3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4),
  test_nr = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 
              1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 2, 2),
  region = c("A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", 
             "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", 
             "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D"),
  test_value = c(3, 1, 2, 2, 2, 1, 2, 2, 3, 2, 2, 3, 2, 1, 2, 2, 1, 2, 3, 
                 4, 2, 1, 1, 2, 1, 1, 1, 1, 2, 2, 3, 2, 2, 2, 99, 99, 3, 3, 3, 3)
)

# named vector for heatmap
cols <-  c("1" = "green", 
           "2" = "darkgreen", 
           "3" = "orange", 
           "4" = "red",
           "99" = "black")
labels_legend <- c("1" = "very good", 
                   "2" = "good", 
                   "3" = "not so good", 
                   "4" = "bad", 
                   "99" = "NA")

df <- df %>% 
  filter(test_id==1)
```

```{r heatmap, fig.show="hold", fig.cap='Rectangle and Heatmap', fig.subcap=c('LEFT', 'RIGHT'), out.width='50%', fig.align = "center"}
drawBox(x =2, y = 3.5, width = 2.5, height = 1)

ggplot(
  df, 
  aes(region, test_nr)
  ) +
  geom_tile(aes(fill= factor (test_value))) +
  geom_text(aes(label = test_value), size = 10, color = "white") + # text in tiles
  scale_colour_manual(
    values = cols, 
    breaks = c("1", "2", "3", "4", "99"),
    labels = labels_legend,
    aesthetics = c("colour", "fill")
  ) +
  theme(text = element_text(size = 14)) + # this will change all text size
  labs(title =  "Test (Individual heatmap)", x = "Region", y = "Event") +
  labs(fill = "Test") +
  coord_fixed(ratio=1, clip="on") +
  theme(axis.text.y = element_text(face = "bold", size = 12)) +
  theme(axis.text.x = element_text(angle = 0, face = "bold", size = 12)) +
  theme(
    axis.line = element_line(
      colour = "darkblue", 
      size = 1, linetype = "solid"
      )
  )
```

## Information

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM