使用 plot_grid 和 cowplot 删除 NULL 图上的标签
Posted
技术标签:
【中文标题】使用 plot_grid 和 cowplot 删除 NULL 图上的标签【英文标题】:Remove labels on NULL plots using plot_grid and cowplot 【发布时间】:2021-09-06 15:26:37 【问题描述】:我正在使用 plot_grid 和 cowplot 在网格中排列图。我需要在网格中有一些“空”图。使用 NULL 工作正常 - 但空间仍然被标记。有没有办法让 NULL 图自动没有标签?我知道我可以手动完成所有绘图标签。
MWE(改编自this page)
library(ggplot2)
df <- data.frame(
x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3, y4 = (1:10)^4
)
p1 <- ggplot(df, aes(x, y1)) + geom_point()
p2 <- ggplot(df, aes(x, y2)) + geom_point()
p3 <- ggplot(df, aes(x, y3)) + geom_point()
p4 <- ggplot(df, aes(x, y4)) + geom_point()
p5 <- ggplot(mpg, aes(as.factor(year), hwy)) +
geom_boxplot() +
facet_wrap(~class, scales = "free_y")
# simple grid
plot_grid(p1, NULL, p3, p4, labels = "AUTO")
我正在寻找您可以通过 plot_grid(p1, NULL, p3, p4, labels = c("A","","B","C")
获得的行为,但我不想单独设置每个情节
【问题讨论】:
【参考方案1】:这是使用修改后的cowplot::plot_grid()
的潜在解决方案:
plot_grid_modified <- function(..., plotlist = NULL, align = c("none", "h", "v", "hv"),
axis = c("none", "l", "r", "t", "b", "lr", "tb", "tblr"),
nrow = NULL, ncol = NULL, rel_widths = 1,
rel_heights = 1, labels = NULL, label_size = 14,
label_fontfamily = NULL, label_fontface = "bold", label_colour = NULL,
label_x = 0, label_y = 1,
hjust = -0.5, vjust = 1.5, scale = 1., greedy = TRUE,
byrow = TRUE, cols = NULL, rows = NULL)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
num_plots <- length(plots)
if (!is.null(cols))
warning("Argument 'cols' is deprecated. Use 'ncol' instead.")
if (!is.null(rows))
warning("Argument 'rows' is deprecated. Use 'nrow' instead.")
scale <- rep_len(scale, num_plots)
if (sum(scale <= 0) > 1)
stop("Argument 'scale' needs to be greater than 0.")
# internally, this function operates with variables cols and rows instead of ncol and nrow
if (!is.null(ncol))
cols <- ncol
if (!is.null(nrow))
rows <- nrow
# calculate grid dimensions
if (is.null(cols) && is.null(rows))
# if neither rows nor cols are given, we make a square grid
cols <- ceiling(sqrt(num_plots))
rows <- ceiling(num_plots/cols)
# alternatively, we know at least how many rows or how many columns we need
if (is.null(cols)) cols <- ceiling(num_plots/rows)
if (is.null(rows)) rows <- ceiling(num_plots/cols)
# if the user wants to layout the plots by column, we use the calculated rows to reorder plots
if (!isTRUE(byrow)) plots <- plots[c(t(matrix(c(1:num_plots, rep(NA, (rows * cols) - num_plots)), nrow = rows, byrow = FALSE)))]
# Align the plots (if specified)
grobs <- align_plots(plotlist = plots, align = align, axis = axis, greedy = greedy)
if ("AUTO" %in% labels)
count <- 0
labels <- c()
for (idx in seq_along(plots))
if (!is.null(unlist(plots[idx])))
count <- count + 1
labels <- c(labels, LETTERS[count])
else
labels <- c(labels, "")
else if ("auto" %in% labels)
count <- 0
labels <- c()
for (idx in seq_along(plots))
if (!is.null(unlist(plots[idx])))
count <- count + 1
labels <- c(labels, letters[count])
else
labels <- c(labels, "")
# label adjustments can be provided globally for all labels
# or individually for each label
hjust <- rep_len(hjust, length(labels))
vjust <- rep_len(vjust, length(labels))
label_x <- rep_len(label_x, length(labels))
label_y <- rep_len(label_y, length(labels))
# calculate appropriate vectors of rel. heights and widths
rel_heights <- rep(rel_heights, length.out = rows)
rel_widths <- rep(rel_widths, length.out = cols)
# calculate the appropriate coordinates and deltas for each row and column
x_deltas <- rel_widths/sum(rel_widths)
y_deltas <- rel_heights/sum(rel_heights)
xs <- cumsum(rel_widths)/sum(rel_widths) - x_deltas
ys <- 1 - cumsum(rel_heights)/sum(rel_heights)
# now place all the plots
p <- ggdraw() # start with nothing
col_count <- 0
row_count <- 1
for (i in 1:(rows*cols))
if (i > num_plots) break
x_delta <- x_deltas[col_count+1]
y_delta <- y_deltas[row_count]
x <- xs[col_count+1]
y <- ys[row_count]
# place the plot
p_next <- grobs[[i]]
if (!is.null(p_next))
p <- p + draw_grob(p_next, x, y, x_delta, y_delta, scale[i])
# place a label if we have one
if (i <= length(labels))
p <- p + draw_plot_label(labels[i], x + label_x[i]*x_delta, y + label_y[i]*y_delta, size = label_size,
family = label_fontfamily, fontface = label_fontface, colour = label_colour,
hjust = hjust[i], vjust = vjust[i])
# move on to next grid position
col_count <- col_count + 1
if (col_count >= cols)
col_count <- 0
row_count <- row_count + 1
p
library(ggplot2)
library(cowplot)
df <- data.frame(
x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3, y4 = (1:10)^4
)
p1 <- ggplot(df, aes(x, y1)) + geom_point()
p2 <- ggplot(df, aes(x, y2)) + geom_point()
p3 <- ggplot(df, aes(x, y3)) + geom_point()
p4 <- ggplot(df, aes(x, y4)) + geom_point()
p5 <- ggplot(mpg, aes(as.factor(year), hwy)) +
geom_boxplot() +
facet_wrap(~class, scales = "free_y")
# simple grid
plot_grid_modified(p1, NULL, p3, p4, labels = "AUTO")
【讨论】:
以上是关于使用 plot_grid 和 cowplot 删除 NULL 图上的标签的主要内容,如果未能解决你的问题,请参考以下文章
我在 R 中使用 cowplot 和 ggplot2 的 plot_grids 缺少网格中的四个图之一?
如何使用`cowplot`软件包中的`plot_grid`对齐地块最后一列中的图例?
R语言使用cowplot包的plot_grid函数将两个ggplot2可视化结果并排组合起来并添加图像标签AB设置组合图像使用共享的图例(shared legend in cowplot)