Я хочу создать массив графиков, используя основание R, каждый из которых имеет свой собственный график вставки, основанный на ggplot, выровненный по правому краю, немного ниже, чем верхний правый, чтобы я мог добавить текст выше.Примерно так (я нарисовал вставки ggplot с помощью MS Paint).

Я думаю, что это возможно при использовании viewports
- аналогично этот вопрос.
# ggplot code for inserts
library(tidyverse)
g1 <- ggplot(data = mtcars, mapping = aes(x = cyl)) +
geom_density(colour = "red") +
theme_void()
g2 <- ggplot(data = mtcars, mapping = aes(x = disp)) +
geom_density(colour = "red") +
theme_void()
g3 <- ggplot(data = mtcars, mapping = aes(x = hp)) +
geom_density(colour = "red") +
theme_void()
g4 <- ggplot(data = mtcars, mapping = aes(x = drat)) +
geom_density(colour = "red") +
theme_void()
g5 <- ggplot(data = mtcars, mapping = aes(x = wt)) +
geom_density(colour = "red") +
theme_void()
g6 <- ggplot(data = mtcars, mapping = aes(x = qsec)) +
geom_density(colour = "red") +
theme_void()
Я попытался поиграть с функциями области просмотра, однако я не могу разместить вставку относительно каждой под фигуры (я думаю, что все они размещены на основеобщее графическое устройство) ...
library(grid)
par(mfrow = c(2, 3))
vps <- baseViewports()
plot(x = mtcars$mpg, y = mtcars$cyl)
pushViewport(vps$figure)
print(g1, vp = viewport(height = unit(0.2, "npc"), width = unit(0.2, "npc"), x = 1, y = 0.8, just = 1))
plot(x = mtcars$mpg, y = mtcars$disp)
pushViewport(vps$figure)
# upViewport()
# popViewport()
print(g2, vp = viewport(height = unit(0.2, "npc"), width = unit(0.2, "npc"), x = 1, y = 0.8, just = 1))
plot(x = mtcars$mpg, y = mtcars$hp)
print(g3, vp = viewport(height = unit(0.2, "npc"), width = unit(0.2, "npc"), x = 1, y = 0.8, just = 1))
plot(x = mtcars$mpg, y = mtcars$drat)
print(g4, vp = viewport(height = unit(0.2, "npc"), width = unit(0.2, "npc"), x = 1, y = 0.8, just = 1))
plot(x = mtcars$mpg, y = mtcars$wt)
print(g5, vp = viewport(height = unit(0.2, "npc"), width = unit(0.2, "npc"), x = 1, y = 0.8, just = 1))
plot(x = mtcars$mpg, y = mtcars$qsec)
print(g6, vp = viewport(height = unit(0.2, "npc"), width = unit(0.2, "npc"), x = 1, y = 0.8, just = 1))
