gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:05:41 24 4
gpt4 key购买 nike

我使用 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

最佳答案

您可以使用 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

关于减少 Rmd PDF 中 ggplot 和标题之间的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66232905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com