Или мы можем создать фальшивый фасет и поместить в него заголовок графика.После этого выполните некоторые настройки, чтобы удалить фасет полосы и уменьшить поле легенды
library(ggplot2)
d <- data.frame(x = 1:2, y = 1:2, z = c("a", "b"))
d$Title <- "My title\n"
# default legend key text
p1 <- ggplot(d, aes(x = x, y = y, fill = z)) +
geom_col() +
facet_grid(~ Title) +
theme(strip.text.x = element_text(hjust = 0, vjust = 1,
size = 14, face = 'bold'),
strip.background = element_blank()) +
theme(legend.margin = margin(5, 0, 0, 0),
legend.box.margin = margin(0, 0, -10, 0)) +
theme(legend.position = "top") +
NULL
# legend key text at the bottom
p2 <- ggplot(d, aes(x = x, y = y, fill = z)) +
geom_col() +
facet_grid(~ Title) +
theme(strip.text.x = element_text(hjust = 0, vjust = 1,
size = 14, face = 'bold'),
strip.background = element_blank()) +
theme(legend.margin = margin(5, 0, 0, 0),
legend.box.margin = margin(0, 0, -10, 0)) +
guides(fill = guide_legend(label.position = "bottom",
title.position = "left", title.vjust = 1)) +
theme(legend.position = "top") +
NULL
library(patchwork)
p1 | p2
![](https://i.imgur.com/isEaQxp.png)
Создано в 2018-10-12 пакетом представ. (v0.2.1.9000)