ggformaEnglish

用 ggplus() 拼图并对齐绘图区

用 ggplus() 对齐四张图

这四张图用 Base R、Lattice 和两种 ggplot2 样式展示同一组身高体重数据。用 nrow 排成两行,再用 gap 在列间增加 4 毫米留白、行间增加 6 毫米。各图的绘图区按行列对齐。

这四张图用 Base R、Lattice 和两种 ggplot2 样式展示同一组身高体重数据。用 nrow 排成两行,再用 gap 在列间增加 4 毫米留白、行间增加 6 毫米。各图的绘图区按行列对齐。
PNG · SVG · PDF · 在相册中查看

先安装 gridGraphics,以便捕获 Base R 图形。把四张原图交给 ggplus(),用 nrow = 2 指定两行,列数会自动算出。gap = c(x = 4, y = 6) 表示列间增加 4 毫米留白,行间增加 6 毫米。

library(ggforma)
library(ggplot2)
library(lattice)
library(patchwork)

# Install gridGraphics for Base R capture.
p1 <- ~plot(women)
p2 <- xyplot(weight ~ height, data = women)
p3 <- ggplot(women, aes(height, weight)) + geom_point()
p4 <- p3 + theme_forma()
figure <- ggplus(p1, p2, p3, p4, nrow = 2, align = "panel",
                 gap = c(x = 4, y = 6))

# Enter figure in the R console to preview it.

render_example <- function(file, device = ragg::agg_png) {
  ggsave(file, figure, width = 190, height = 150, units = "mm",
         device = device, dpi = 144, bg = "white")
}

png.file <- file.path(tempdir(), "composition-alignment.png")
render_example(png.file)
png.file

下载 R 脚本

Base R 图默认使用紧凑边距。绘图代码里自己设的 par() 优先。绘图区的宽或高低于 15 mm 时,提示会给出实际尺寸,并指出哪张图占用了空间。

用同一个对象预览和导出

在 R 控制台运行下面这行。改变 Plots 窗格大小时,会按可用空间重新计算绘图区。

figure

导出时沿用这个对象,只需指定整张图的宽高,无需填写每张子图的 slot。

ggsave("women-aligned.png", figure, width = 190, height = 150,
       units = "mm", dpi = 144, device = ragg::agg_png)
ggsave("women-aligned.svg", figure, width = 190, height = 150,
       units = "mm", device = svglite::svglite)
ggsave("women-aligned.pdf", figure, width = 190, height = 150,
       units = "mm", device = grDevices::cairo_pdf)

固定子图尺寸与 patchwork 拼法

如果每张子图需要固定的物理尺寸,或想查看对齐前后的边界坐标,可以用 align_forma()。| 把两张图并排,/ 把两排图上下放在一起,+ 可以加总标题。也可以把 ggplus() 生成的整组图继续与其他图拼接。

plan <- align_forma(p1, p2, p3, p4, axis = "both", slot = c(80, 65))
p <- plan$plots
fixed <- ((p[[1]] | p[[2]]) / (p[[3]] | p[[4]])) +
  plot_annotation(title = "Height and weight")
ggsave("women-fixed.pdf", fixed, width = 174, height = 148,
       units = "mm", device = grDevices::pdf)

对齐雷达图与 Base R 散点图

左图用雷达图比较两组诊所的平均得分,右图用 Base R 散点图展示各家诊所的得分。雷达网格保持圆形,两张图的浅色绘图区上下对齐。

左图用雷达图比较两组诊所的平均得分,右图用 Base R 散点图展示各家诊所的得分。雷达网格保持圆形,两张图的浅色绘图区上下对齐。
PNG · SVG · PDF · 在相册中查看

Base R 散点图用单侧公式传入。先安装 gridGraphics,ggplus() 才能捕获 Base R 图形。

library(ggforma)
library(ggplot2)

clinics <- data.frame(
  Network = rep(c("North", "South"), each = 8),
  Coverage = c(69, 78, 85, 79, 89, 85, 82, 91, 53, 62, 64, 69, 72, 75, 77, 78),
  Testing = c(60, 62, 65, 70, 73, 65, 72, 75, 68, 70, 74, 75, 74, 82, 83, 89),
  Outreach = c(77, 83, 86, 82, 89, 88, 78, 85, 70, 76, 78, 72, 81, 77, 83, 81),
  Followup = c(62, 77, 75, 63, 85, 80, 73, 88, 54, 61, 58, 69, 67, 72, 70, 73)
)
profiles <- aggregate(. ~ Network, clinics, mean)
colours <- setNames(forma_palette(9)[c(6, 7)], c("North", "South"))
radar <- ggradar(profiles, vars = c(Coverage, Testing, Outreach, Followup),
                 group = Network, limits = c(0, 100), grid = "circle") +
  labs(title = "Average clinic profiles") +
  theme(panel.background = element_rect(fill = "#EEF6F4", colour = NA))

# Install gridGraphics for Base R capture.
scatter <- ~{
  par(mar = c(3.6, 3.8, 2.5, 0.8), mgp = c(2.15, 0.65, 0),
      las = 1, cex = 0.85, tcl = -0.2)
  plot(Followup ~ Coverage, data = clinics, type = "n", bty = "n",
       main = "Clinic scores · Base R", xlab = "Coverage score", ylab = "Follow-up score")
  region <- par("usr")
  rect(region[1], region[3], region[2], region[4], col = "#F1F5FA", border = NA)
  points(Followup ~ Coverage, data = clinics, pch = 16, col = colours[clinics$Network])
}

figure <- ggplus(radar, scatter, nrow = 1, align = "panel", gap = 4)

# Enter figure in the R console to preview it.
render_example <- function(file, device = ragg::agg_png) {
  ggsave(file, figure, width = 150, height = 110, units = "mm",
         device = device, dpi = 144, bg = "white")
}

png.file <- file.path(tempdir(), "composition-radar-base.png")
render_example(png.file)
png.file

下载 R 脚本

用 nrow = 1 把两张图排成一行,设 align = "panel" 对齐绘图区,gap = 4 增加 4 毫米留白。两张图都应只有一个面板,其中一张保持固定宽高比。左右可以互换,坐标轴和图形边距仍占各自的空间。

对齐雷达图与 Lattice 散点图

左图用雷达图比较两组诊所的平均得分,右图用 Lattice 散点图展示各家诊所的得分。雷达网格保持圆形,两张图的浅色绘图区上下对齐。

左图用雷达图比较两组诊所的平均得分,右图用 Lattice 散点图展示各家诊所的得分。雷达网格保持圆形,两张图的浅色绘图区上下对齐。
PNG · SVG · PDF · 在相册中查看

直接传入 xyplot() 绘制的 Lattice 图,并设 aspect = "fill",让散点图的绘图区高度可以调整。

library(ggforma)
library(ggplot2)
library(lattice)
library(grid)

clinics <- data.frame(
  Network = rep(c("North", "South"), each = 8),
  Coverage = c(69, 78, 85, 79, 89, 85, 82, 91, 53, 62, 64, 69, 72, 75, 77, 78),
  Testing = c(60, 62, 65, 70, 73, 65, 72, 75, 68, 70, 74, 75, 74, 82, 83, 89),
  Outreach = c(77, 83, 86, 82, 89, 88, 78, 85, 70, 76, 78, 72, 81, 77, 83, 81),
  Followup = c(62, 77, 75, 63, 85, 80, 73, 88, 54, 61, 58, 69, 67, 72, 70, 73)
)
profiles <- aggregate(. ~ Network, clinics, mean)
colours <- setNames(forma_palette(9)[c(6, 7)], c("North", "South"))
radar <- ggradar(profiles, vars = c(Coverage, Testing, Outreach, Followup),
                 group = Network, limits = c(0, 100), grid = "circle") +
  labs(title = "Average clinic profiles") +
  theme(panel.background = element_rect(fill = "#EEF6F4", colour = NA))

scatter <- xyplot(Followup ~ Coverage, data = clinics, aspect = "fill",
                  main = "Clinic scores · Lattice",
                  xlab = "Coverage score", ylab = "Follow-up score",
                  panel = function(x, y, subscripts, ...) {
                    grid.rect(gp = gpar(fill = "#F1F5FA", col = NA))
                    panel.xyplot(x, y, pch = 16, col = colours[clinics$Network[subscripts]], ...)
                  }, par.settings = list(axis.line = list(col = "transparent")))

figure <- ggplus(radar, scatter, nrow = 1, align = "panel", gap = 4)

# Enter figure in the R console to preview it.
render_example <- function(file, device = ragg::agg_png) {
  ggsave(file, figure, width = 150, height = 110, units = "mm",
         device = device, dpi = 144, bg = "white")
}

png.file <- file.path(tempdir(), "composition-radar-lattice.png")
render_example(png.file)
png.file

下载 R 脚本

用 nrow = 1 把两张图排成一行,设 align = "panel" 对齐绘图区,gap = 4 增加 4 毫米留白。两张图都应只有一个面板,其中一张保持固定宽高比。左右可以互换,坐标轴和图形边距仍占各自的空间。