grid.arrange 中的错误 -rangeGrob() 函数

gre*_*hie 5 r ggplot2

我有两个图 p1 和 p2,我试图使用 grid.arrage 绘制它们。我的代码如下所示:

grid.arrange(p1, p2, ncol=2,
    top = textGrob("Distribution across each day of the week", 
    gp = gpar(fontface = "bold", cex = 1.5)),
    bottom = "Day of the week")
Run Code Online (Sandbox Code Playgroud)

但是,当我运行这个时,我看到一个错误"Error in arrangeGrob(...) : could not find function "textGrob"

当我只跑步时grid.arrange(p1, p2, ncol=2),它运行得很好。但没有任何标签和标题。但是,我无法理解我的代码有什么问题。我都尝试过main=...,但top=...都不起作用。

有什么建议么?

law*_*yeR 2

这是两个简单的 png 文件。

\n\n
value <- c(0, 1, 20, 3, 3, 0, 0, 5, 2, 5, 2, 7)\nnames.arg =c("0-15","15-19","20-24","25-29","30-34",\n             "35-39","40-44","45- 49","50-54","55-59","60-64","65 Jahre oder \xc3\x84lter")\ndf <- data.frame(names.arg = names.arg, value = value)\n\np1 <- ggplot(df, aes(x=names.arg, y=value)) + geom_bar(stat = "identity")\nsave(p1, file = "p1.png")\n\nvalue2 <- c(0, 1, 20, 3, 3, 0, 0, 5, 2, 5, 2, 7)\nnames2 =c("0-15","15-19","20-24","25-29","30-34",\n             "35-39","40-44","45- 49","50-54","55-59","60-64","65 Jahre oder \xc3\x84lter")\ndf2 <- data.frame(names = names2, value = value2)\n\np2 <- ggplot(df2, aes(x=names, y=value)) + geom_bar(stat = "identity", fill = "red")\nsave(p2, file = "p2.png")\n
Run Code Online (Sandbox Code Playgroud)\n\n

当您将它们组合起来时,top =bottom =参数可以正常工作:

\n\n
grid.arrange(p1, p2, ncol=1, top = "Example", bottom = "Sample")\n
Run Code Online (Sandbox Code Playgroud)\n\n

在此输入图像描述

\n\n

根据评论进行编辑

\n\n

在通话之外创建标题grid.arrange()

\n\n
title <- textGrob("Distribution across each day of the week", gp = gpar(fontface = "bold", cex = 1.5))\n
Run Code Online (Sandbox Code Playgroud)\n\n

并修改调用:

\n\n
grid.arrange(p1, p2, ncol=1, top = title, bottom = "Sample")\n
Run Code Online (Sandbox Code Playgroud)\n\n

在此输入图像描述

\n