减少 Rmd PDF 中 ggplot 和标题之间的空白

时间:2021-02-16 22:10:42

标签: r ggplot2 latex r-markdown

我使用 ggarrange 在 RMD PDF 中有以下两个图。他们很好,但我对标题和实际情节之间的大空白有点恼火。有什么办法可以减少这个空间吗?

---
title: Report
author: My Name
date: "`r format(Sys.Date() - 30, '%B %Y')`"
params:
  logo: logo.jpg
  cover: cover.png
  iblue: 6d1d26
  igray: ffffff
documentclass: article
classoption: 
  - twocolumn
fontsize: 15
papersize: a4paper
output: 
  IReports::businessReport:
    keep_tex: TRUE
    latex_engine: xelatex
    resetStyleFiles: FALSE
header-includes: 
  - \newcommand{\logo}{`r gsub("_", "\\_", params$logo)`}
  - \newcommand{\cover}{`r gsub("_", "\\_", params$cover)`}
  - \newcommand{\iblue}{`r params$iblue`}
  - \newcommand{\igray}{`r params$igray`}
  - \usepackage{floatrow}
  - \floatsetup[figure]{capposition=top}
  - \floatsetup[table]{capposition=top}
include-before:
  - \renewcommand{\contentsname}{Contents Page}
  - \renewcommand{\pagename}{Page}
---

```{r setup, include = FALSE}
# packages
library(dplyr)
library(knitr)
library(xtable)

# settings
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
```


```{r fig6, fig.align='center', fig.cap="Currency (left) and Asset (Right) Allocation \\label{Figure6}", fig.width=5,fig.height=3, fig.pos='!h'}

library(tidyverse)
library(ggpubr)

# Dataframes

currency_breakdown <- structure(list(Currency = structure(1:4, .Label = c("ZAR", "USD", 
"EUR", "Other"), class = "factor"), `Percent (%)` = c(52.25, 
41.87, 3.11, 2.77), Key = c("ZAR [52.25%]", "USD [41.87%]", "EUR [3.11%]", 
"Other [2.77%]")), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"))

asset_breakdown <- structure(list(Asset = structure(c(4L, 3L, NA, 1L), .Label = c("Cash", 
"Money Market Securities", "Treasuries", "Agencies & Supras"), class = "factor"), 
    `Percent (%)` = c(1.02, 34.61, 33.06, 31.31), Key = c("Agencies & Supras [1.02%]", 
    "Treasuries [34.61%]", "Money Market [33.06%]", "Cash [31.31%]"
    )), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"))

# Plots

currency_breakdown_plot <- ggplot(currency_breakdown, aes("", `Percent (%)`, fill = Key)) + 
  geom_bar(stat = "identity", color = "white", size = 1) +
  coord_polar(theta = "y") +
  scale_fill_manual(values = c('#063A5B','#bbacaf','#c7af76','#7c3042')) +
  labs(colour = NULL) +
  theme_void() +
  theme(legend.position = c(.5,0),legend.title = element_blank(),
        legend.text=element_text(size=7)) +
  guides(fill = guide_legend(nrow = 2, override.aes = list(size = 1)))

asset_breakdown_plot <- ggplot(asset_breakdown, aes("", `Percent (%)`, fill = Key)) + 
  geom_bar(stat = "identity", color = "white", size = 1) +
  coord_polar(theta = "y") +
  scale_fill_manual(values = c('#063A5B','#bbacaf','#c7af76','#7c3042'),
                    labels = function(x) str_wrap(x, width = 20)) +
  labs(colour = NULL) +
  theme_void() +
  theme(legend.position = c(.5,0),legend.title = element_blank(),
        legend.text=element_text(size=7)) +
  guides(fill = guide_legend(nrow = 2,override.aes = list(size = 1)))

# ggarrange

ggarrange(currency_breakdown_plot, asset_breakdown_plot, nrow = 1)

```

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以在 theme 中使用 ggplot 选项。在 this answer 之后,您可以将其添加到您的代码中:plot.margin=unit(c(-0.20,0,0,0), "null")

更改代码:

asset_breakdown_plot <- ggplot(asset_breakdown, aes("", `Percent (%)`, fill = Key)) + 
  geom_bar(stat = "identity", color = "white", size = 1) +
  coord_polar(theta = "y") +
  scale_fill_manual(values = c('#063A5B','#bbacaf','#c7af76','#7c3042'),
                    labels = function(x) str_wrap(x, width = 20)) +
  labs(colour = NULL) +
  theme_void() +
  theme(legend.position = c(.5,0),legend.title = element_blank(),
        legend.text=element_text(size=7), plot.margin=unit(c(-0.20,0,0,0), "null")) +
  guides(fill = guide_legend(nrow = 2,override.aes = list(size = 1)))

# ggarrange

ggarrange(currency_breakdown_plot, asset_breakdown_plot, nrow = 1)

-输出

enter image description here