r - 在函数中使用 ggplot2 时出现 ArrangeGrob 错误

标签 r ggplot2 environment-variables

我有以下使用 ggplot 和 gridExtra 包的函数:

new_func <- function(df, df2, df3, output.proj, typenames) {

    require(ggplot2)
        require(gridExtra)

        plot1 <- ggplot(aes(x=Procs2, y=Procs1,
                              fill=P),environment = environment(),data=df) + geom_tile() +
            xlab(colnames(df)[2]) +
            ylab(colnames(df)[1]) +
            theme(panel.grid.major = element_blank(),
                  panel.grid.minor = element_blank(),
                  panel.background = element_blank(),
                  plot.margin=unit(c(0.5,0.5,-0.5,1.5), "cm")) +
            geom_text(aes(fill = P, 
                          label = P),size=3) +
            theme(axis.text.x = element_text(angle=30, vjust=0.5)) +
            scale_fill_gradient(low = "gray37", high = "white") 

        plot2 <- ggplot(df2, aes(Proc, Loc)) + geom_boxplot(aes(fill=Proc),
                                                              environment = environment()) + 
            stat_boxplot(geom ='errorbar') +
            scale_colour_manual(breaks = sort(typenames),
                              labels = sort(typenames),
                              values = colorRampPalette(c("blue","goldenrod1"))(6) ) +
            scale_fill_manual(breaks = sort(typenames),
                              labels = sort(typenames),
                              values = colorRampPalette(c("blue","goldenrod1"))(6) ) +
            theme(axis.text.x = element_text(angle=45, vjust=0.5)) +    
            geom_text(aes(y=max(Loc)+0.5,
                          label =lab ),size=2.5,
                     col=rep(colorRampPalette(c("blue","goldenrod1"))(6),each=22) ) +
            theme(axis.line=element_line(), 
                  panel.grid.major = element_blank(),
                  panel.grid.minor = element_blank(),
                  panel.background = element_blank(),
                  legend.position="none")          


        plot3 <- ggplot(df3, aes(x=Proc, y=types),environment = environment()) + 
            geom_bar(stat="identity", aes(fill=Proc),width=0.5)  + 
            facet_wrap(~Groc,nrow=2, scales="fixed") + # fixed
            theme(axis.text.x = element_blank() )+#element_text(angle=90, vjust=0)) +
            theme(axis.line=element_line(), 
                  panel.grid.major = element_blank(),
                  panel.grid.minor = element_blank(),
                  panel.background = element_blank()) + 
            scale_fill_manual( breaks = df3$Proc,
                              labels = df3$Proc,
                              values = c("red", colorRampPalette(c("blue","yellow"))(6))) 


        pdf(paste(output.proj,".pdf",sep=""),width=10)
        grid.arrange(arrangeGrob(plot1, plot2,ncol=2, nrow=1), plot3, nrow=2,
                     environment = environment())
        dev.off()
}

当我调用这个函数时:

new_func(df,df2,df3,"test",typenames)

我收到以下错误:

Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : input must be grobs!

有什么办法可以让我解决这个问题,同时保留 new_func 中的功能吗? ?

数据

df <- structure(list(Procs1 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 5L), .Label = c("type1", "type2", 
"type3", "type4", "type5", "type6"), class = "factor"), 
    Procs2 = structure(c(2L, 3L, 4L, 5L, 6L, 3L, 4L, 5L, 6L, 
    4L, 5L, 6L, 5L, 6L, 6L), .Label = c("type1", "type2", 
    "type3", "type4", "type5", "type6"), class = "factor"), 
    P = c(0.0057, 0.026, 6e-04, 0.046, 0.19, 0.14, 0.96, 0.06, 
    0.063, 0.033, 0.56, 0.85, 0.011, 0.054, 0.76)), .Names = c("Procs1", 
"Procs2", "P"), row.names = c(NA, -15L), class = "data.frame")

df2 <- structure(list(Loc = c(1, 1, 0, 2, 0, -1, 2, 1, 0, 1, 3, 0, 
0, 0, -1, 0, -1, 1, -1, 1, -1, -2, 1, 0, -2, 1, 0, -1, -2, 2, 
0, -1, 1, 0, -1, 0, -1, -1, -1, 1, -1, 0, -4, -2, 1, 1, 0, 1, 
0, -1, 2, 1, -2, -1, 3, 0, 0, 0, -1, -1, -1, 1, -1, 0, -1, -2, 
0, 0, 0, 1, 0, -1, 1, 1, -1, -1, 1, 0, 0, -1, -1, -1, -1, 0, 
-1, 0, -2, -2, 1, 1, 0, 1, 0, -1, 2, 1, -2, -1, 2, 0, 0, 0, -1, 
0, -1, 1, -1, 1, -1, -2, 2, 0, 0, 1, 0, 0, 2, 2, -2, -1, 1, 0, 
-1, 0, -1, 0, 0, 0, 0, 0, -2, -2), Proc = structure(c(2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L), .Label = c("ref", "type1", "type2", "type3", "type4", 
"type5", "type6"), class = "factor"), lab = c("1.21", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "1.4", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "1.22", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "0.953", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "1.13", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "1.15", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "")), .Names = c("Loc", "Proc", 
"lab"), row.names = c(NA, -132L), class = "data.frame")

df3 <- structure(list(Groc = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L), .Label = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "16", "17", "18", "19", "20", "21", "22"), class = "factor"), 
    Proc = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 
    5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 
    6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 
    7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 
    2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 
    5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("ref", 
    "type1", "type2", "type3", "type4", "type5", "type6"
    ), class = "factor"), types = structure(c(3L, 2L, 2L, 2L, 
    3L, 2L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 5L, 3L, 3L, 
    3L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 1L, 5L, 1L, 2L, 1L, 1L, 
    5L, 4L, 3L, 4L, 4L, 4L, 3L, 2L, 2L, 2L, 4L, 3L, 4L, 4L, 3L, 
    2L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 3L, 1L, 3L, 2L, 3L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 
    3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 
    2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 4L, 3L, 4L, 4L, 4L, 3L, 
    4L, 1L, 2L, 5L, 2L, 3L, 2L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L
    ), .Label = c("2", "3", "4", "5", "6"), class = "factor")), .Names = c("Groc", 
"Proc", "types"), row.names = c(NA, -154L), class = "data.frame")

typenames <- c("type1", "type2", "type3", "type4", "type5", "type6")

最佳答案

您传递给grid.arrange环境似乎被解释为非grob。当我注释掉它时(也注释掉 dev.off),您的 new_func 函数就可以工作了。

  grid.arrange(arrangeGrob(plot1, plot2, ncol=2, nrow=1), 
               plot3, nrow=2
              #, environment = environment()
  )

注释掉环境后,它会产生: enter image description here

关于r - 在函数中使用 ggplot2 时出现 ArrangeGrob 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21343372/

相关文章:

r - 如何找到未排序的数据帧片段

r - 按列名中的模式融化数据帧

r - 这种类型的情节可以用ggplot2完成吗?

makefile - 在make文件中分割变量

javascript - 在 React 项目中保护 .env 参数

java - 我应该在环境变量 CLASSPATH 中有什么

r - 使用ifelse在R中创建新变量,但收到错误消息意外的 '='

r - 将数据框中的因子列转换为数字类型列

r - 在美国 map 上绘制数值

r - 在 ggplot2 直方图中的图例下插入表格