序言
我在
ggplot2
中有 2 个图,想将它们放入 RMarkdown 文档中。我主要使用 grid.arrange
来做到这一点。但由于这些图的高度完全不同,我需要使用 heights = c(0.7, 1)
进行调整
问题
调整高度会在文档中留下大量空白空间
目标
我想以整齐的方式并排整合(放置)这些图,而不用巨大的空间分隔组合图和后面的文本。
代码
---
title: ""
output: pdf_document
geometry:
- margin=1in
- headheight=14pt
fontsize: 11pt
---
\noindent
\\```{r setup, include=FALSE, cache=FALSE}
knitr::opts_chunk$set(
fig.path = '', # No separate figure folders
echo = FALSE, # Show code chunks
warning = FALSE, # Suppress warnings
message = FALSE # Suppress messages
)
\\```
\section{Section 01}
\\```{r plot_data, cache=TRUE}
# Create data frame
data <- data.frame(
labels = c(LETTERS[1:8]),
values = c(88, 61, 56, 77, 83, 63, 42, 60)
)
\\```
\\```{r plotgrid, dependson="plot_data", fig.cap=NULL}
library(tidyverse)
data_nude_bar <- as.tibble(data)
data_nude_bar <- data_nude_bar %>%
mutate(
num = rev(row_number()),
store_lower = 25,
store_upper = values,
text_bg_col = "white",
text_bg_outline = "grey20"
)
# Create the chart
bar_height <- 0.4
p_bar <- ggplot(data_nude_bar) +
ggchicklet::geom_rrect(
# mid rec base
aes(
xmin = store_lower, xmax = 100,
ymin = num - bar_height, ymax = num + bar_height,
fill = "#b9c7a8", alpha = .5
),
radius = grid::unit(1, "mm")
) +
ggchicklet::geom_rrect(
# mid rec fill
aes(
xmin = store_lower, xmax = store_upper,
ymin = num - bar_height, ymax = num + bar_height,
fill = "#8ca470"
),
radius = grid::unit(1, "mm")
) +
ggchicklet::geom_rrect(
# left rec
aes(
xmin = -3, xmax = 24,
ymin = num - bar_height, ymax = num + bar_height,
fill = "gray20", colour = text_bg_outline
),
radius = grid::unit(1, "mm")
) +
scale_fill_identity() +
scale_color_identity() +
geom_text(
# trait name
aes(
label = labels, x = -1, y = num,
hjust = 0, color = "white"
),
size = 2
) +
geom_label(
aes(label = values,x = 105, y = num),
hjust = 0.5, size = 2.5, label.padding = unit(3, "pt"),
color = "grey30", fill = "#edecea",
label.r = unit(3, "pt")
) +
coord_cartesian(clip = "off", xlim = c(0, 100)) +
theme_minimal() +
theme(
plot.margin = margin(r = 15, unit = "pt"),
legend.position = "none",
panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
plot.background = element_rect(
color = "grey30",
fill = "#edecea"
)
)
data_circular_bar <- as.tibble(data) %>%
mutate(
bar_color = case_when(
values < 62 ~ "#de583f",
values < 85 ~ "#759d9f",
values >= 85 ~ "#4b7d84"
),
labels = factor(labels, levels = labels),
labelnum = factor(
c("01", "02", "03", "04", "05", "06", "07", "08"),
levels = c("01", "02", "03", "04", "05", "06", "07", "08")
)
)
scaling_factor <- max(100)
p_circular <- ggplot(data_circular_bar %>% mutate(values = values / scaling_factor),
aes(labels, values, fill = bar_color)) +
geom_col(width = 1, aes(color = after_scale(fill))) +
annotate("linerange", ymin = 0, ymax = 1.02, x = seq(nrow(data)) - 0.5,
color = "#494949") +
geom_hline(yintercept = c(0, 2, 4, 6, 8) / 10, color = "#494949", alpha = 0.2) +
geom_hline(yintercept = 1.02, color = "#d9d9d9", linewidth = 2) +
geom_hline(yintercept = .98, color = "#494949") +
geom_label(aes(y = 1, label = labelnum), fill = "#edecea", color = "#494949",
size = 4) +
scale_fill_identity() +
coord_polar(start = -pi/8) +
theme_void() +
scale_y_continuous(limits = c(0, 1.1), expand = c(0, 0)) +
theme(plot.background = element_rect(fill = "#edecea", color = "grey30"))
library(gridExtra)
library(grid)
grid.arrange(p_circular, p_bar, ncol = 2, heights = c(.7,1), widths = c(3,7))
\\```
Text after plot
迄今为止的努力
\vspace{-20pt}
:不起作用,结果仍然相同heights
参数使空间消失,但地块的高度变成原来的高度,看起来很糟糕patchwork
也仍然有完全相同的问题基于此,我需要帮助将这些图并排放置,而不留下大量空白空间
通过拼凑,您可以指定“空白”的高度(
#
),例如
library(patchwork)
p_circular + p_bar + plot_layout(heights = c(0.5, 1), widths = c(0.7, 1),
nrow = 1,
design = "##
AB")
library(patchwork)
p_circular + p_bar + plot_layout(heights = c(0.1, 1), widths = c(0.7, 1),
nrow = 1,
design = "##
AB")
将其与 rmarkdown 标头中的
fig.height
结合起来,您应该能够解决您的问题,例如
```{r plotgrid, dependson="plot_data", fig.cap=NULL, fig.height=2}
...
library(patchwork)
p_circular + p_bar + plot_layout(heights = c(0.5, -0.05), widths = c(0.7, 1),
nrow = 1,
design = "AB
##")
另外,我刚刚记得你周五的回复(https://stackoverflow.com/a/79277334/12957340) - 我现在将编辑该答案以使其更适合目的。