You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用grid.arrange排列QQ图时遇'only grobs allowed in glist'错误求助

Fixing the "only 'grobs' allowed in 'glist'" Error with grid.arrange() and QQ Plots

Hey there! Let's break down why you're hitting this error and how to fix it quickly.

The Root of the Problem

The base R function qqnorm() doesn't return a graphical object (grob) — it just draws the plot directly to your active graphics device. When you assign qq_clump_thickness <- qqnorm(...), that variable is actually storing some statistical output from the QQ plot calculation, not the plot itself. Since grid.arrange() only works with grobs, it throws that error when you pass these non-grob variables to it.

ggplot2 creates plot objects that are native grobs, so they play nicely with grid.arrange(). Here's how to rewrite your code with ggplot2:

First, make sure you have the package installed and loaded:

install.packages("ggplot2")
library(ggplot2)
library(gridExtra) # for grid.arrange()

Then create your QQ plots as ggplot objects (we'll use a helper function to avoid repeating code):

# Helper function to generate consistent QQ plots
make_qq_plot <- function(data_col, plot_title) {
  ggplot(data, aes(sample = {{data_col}})) +
    stat_qq() +
    stat_qq_line(color = "darkred") + # Adds reference line for normality check
    ggtitle(plot_title) +
    theme_bw()
}

# Create each QQ plot
qq_clump_thickness <- make_qq_plot(clump_thickness, "Clump Thickness")
qq_uniformity_of_cell_size <- make_qq_plot(uniformity_of_cell_size, "Uniformity of Cell Size")
qq_uniformity_of_cell_shape <- make_qq_plot(uniformity_of_cell_shape, "Uniformity of Cell Shape")
qq_marginal_adhesion <- make_qq_plot(marginal_adhesion, "Marginal Adhesion")
qq_single_epithelial_cell_size <- make_qq_plot(single_epithelial_cell_size, "Single Epithelial Cell Size")
qq_bare_nuclei <- make_qq_plot(bare_nuclei, "Bare Nuclei")
qq_bland_chromatin <- make_qq_plot(bland_chromatin, "Bland Chromatin")
qq_normal_nucleoli <- make_qq_plot(normal_nucleoli, "Normal Nucleoli")
qq_mitosis <- make_qq_plot(mitosis, "Mitosis")

# Arrange all plots together
grid.arrange(qq_clump_thickness, qq_uniformity_of_cell_size, 
             qq_uniformity_of_cell_shape, qq_marginal_adhesion, 
             qq_single_epithelial_cell_size, qq_bare_nuclei, 
             qq_bland_chromatin, qq_normal_nucleoli, qq_mitosis,
             top = textGrob("QQ Plots", gp = gpar(fontsize=15, font=1)))

Solution 2: Convert Base R Plots to Grobs

If you prefer sticking with base R's qqnorm(), you can use the gridGraphics package to convert base plots into grobs that grid.arrange() can understand:

First install and load the required packages:

install.packages("gridGraphics")
library(gridGraphics)
library(gridExtra)

Then modify your code to capture each base plot as a grob:

# Helper function to turn base QQ plots into grobs
base_qq_to_grob <- function(data_col, plot_title) {
  grid.echo({
    qqnorm(data_col, main = plot_title)
    qqline(data_col) # Add reference line (optional but useful)
  })
  grid.grab() # Capture the plot as a grob
}

# Create grobs for each QQ plot
qq_clump_thickness <- base_qq_to_grob(data$clump_thickness, "Clump Thickness")
qq_uniformity_of_cell_size <- base_qq_to_grob(data$uniformity_of_cell_size, "Uniformity of Cell Size")
qq_uniformity_of_cell_shape <- base_qq_to_grob(data$uniformity_of_cell_shape, "Uniformity of Cell Shape")
qq_marginal_adhesion <- base_qq_to_grob(data$marginal_adhesion, "Marginal Adhesion")
qq_single_epithelial_cell_size <- base_qq_to_grob(data$single_epithelial_cell_size, "Single Epithelial Cell Size")
qq_bare_nuclei <- base_qq_to_grob(data$bare_nuclei, "Bare Nuclei")
qq_bland_chromatin <- base_qq_to_grob(data$bland_chromatin, "Bland Chromatin")
qq_normal_nucleoli <- base_qq_to_grob(data$normal_nucleoli, "Normal Nucleoli")
qq_mitosis <- base_qq_to_grob(data$mitosis, "Mitosis")

# Arrange the grobs into a grid
grid.arrange(qq_clump_thickness, qq_uniformity_of_cell_size, 
             qq_uniformity_of_cell_shape, qq_marginal_adhesion, 
             qq_single_epithelial_cell_size, qq_bare_nuclei, 
             qq_bland_chromatin, qq_normal_nucleoli, qq_mitosis,
             top = textGrob("QQ Plots", gp = gpar(fontsize=15, font=1)))

Either approach should resolve the error and let you arrange all your QQ plots neatly!

内容的提问来源于stack exchange,提问作者Sql_Pete_Belfast

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:02:51