在图形设备的子图形中组合 base 和 ggplot 图形
Combine base and ggplot graphics in sub figures of graphic device
我想使用 base R 生成一组图,每个图都有自己的基于 ggplot 的插入图,向右对齐,略低于右上角,以便我可以在上面添加一些文本。像这样的东西(我用 MS Paint 手绘了 ggplot 插入)。
我认为这可能使用 viewports
- 类似于 this 问题。
# 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))
一个解决方案(不涉及在您想要制作的地块上做出妥协)是使用 viewpoint()
中的 layout
参数为子地块设置一个网格,覆盖par(mfrow)
/par(mfcol)
网格。
将视点网格设置为 par(mfrow)
维度的倍数可以让您将子图很好地放置在所需的网格位置。视点网格的比例将决定子图的大小 - 因此更大的网格将导致更小的子图。
# base R plots
par(mfrow = c(2, 3))
plot(x = mtcars$mpg, y = mtcars$cyl)
plot(x = mtcars$mpg, y = mtcars$disp)
plot(x = mtcars$mpg, y = mtcars$hp)
plot(x = mtcars$mpg, y = mtcars$drat)
plot(x = mtcars$mpg, y = mtcars$wt)
plot(x = mtcars$mpg, y = mtcars$qsec)
# set up viewpoint grid
library(grid)
pushViewport(viewport(layout=grid.layout(20, 30)))
# add ggplot subplots (code for these objects in question) at `layout.pos.row`, `layout.pos.col`
print(g1, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 2, layout.pos.col = 9))
print(g2, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 2, layout.pos.col = 19))
print(g3, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 2, layout.pos.col = 29))
print(g4, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 12, layout.pos.col = 9))
print(g5, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 12, layout.pos.col = 19))
print(g6, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 12, layout.pos.col = 29))
如果您有简单的基本 R 图,那么另一种方法是使用 ggplotify 将基本图转换为 ggplot,然后使用 cowplot 或 patchwork 进行放置。我无法为我想要的图工作,它使用比上面虚拟示例中的更复杂的一组绘图函数(在 base R 中)。
我想使用 base R 生成一组图,每个图都有自己的基于 ggplot 的插入图,向右对齐,略低于右上角,以便我可以在上面添加一些文本。像这样的东西(我用 MS Paint 手绘了 ggplot 插入)。
我认为这可能使用 viewports
- 类似于 this 问题。
# 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))
一个解决方案(不涉及在您想要制作的地块上做出妥协)是使用 viewpoint()
中的 layout
参数为子地块设置一个网格,覆盖par(mfrow)
/par(mfcol)
网格。
将视点网格设置为 par(mfrow)
维度的倍数可以让您将子图很好地放置在所需的网格位置。视点网格的比例将决定子图的大小 - 因此更大的网格将导致更小的子图。
# base R plots
par(mfrow = c(2, 3))
plot(x = mtcars$mpg, y = mtcars$cyl)
plot(x = mtcars$mpg, y = mtcars$disp)
plot(x = mtcars$mpg, y = mtcars$hp)
plot(x = mtcars$mpg, y = mtcars$drat)
plot(x = mtcars$mpg, y = mtcars$wt)
plot(x = mtcars$mpg, y = mtcars$qsec)
# set up viewpoint grid
library(grid)
pushViewport(viewport(layout=grid.layout(20, 30)))
# add ggplot subplots (code for these objects in question) at `layout.pos.row`, `layout.pos.col`
print(g1, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 2, layout.pos.col = 9))
print(g2, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 2, layout.pos.col = 19))
print(g3, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 2, layout.pos.col = 29))
print(g4, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 12, layout.pos.col = 9))
print(g5, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 12, layout.pos.col = 19))
print(g6, vp = viewport(height = unit(0.2, "npc"), width = unit(0.05, "npc"),
layout.pos.row = 12, layout.pos.col = 29))
如果您有简单的基本 R 图,那么另一种方法是使用 ggplotify 将基本图转换为 ggplot,然后使用 cowplot 或 patchwork 进行放置。我无法为我想要的图工作,它使用比上面虚拟示例中的更复杂的一组绘图函数(在 base R 中)。