【问题标题】:ggplot2 not recognizing a column in a function callggplot2无法识别函数调用中的列
【发布时间】:2025-11-30 20:55:02
【问题描述】:

当我在控制台中运行时,我的代码运行良好并绘制了图表。但是,当我将其放入函数中时,我收到一条错误消息,指出无法找到其中一列。

"Error in tapply(X = X, INDEX = x, FUN = FUN, ...) :   
object 'm.raw' not found"

首先,我用于绘图的数据框如下所示

DF =
lbls    raw.1   raw.2   raw.3   m.raw   imp.1   imp.2   imp.3   m.imp
A1  0.020   0.031   0.032   0.028   11.266  17.408  17.622  15.432
A2  0.023   0.024   0.036   0.028   12.732  13.452  20.034  15.406
A3  0.002   0.022   0.016   0.013   1.344   12.111  9.011   7.489
A4  0.015   0.031   0.021   0.023   8.641   17.575  11.790  12.669
A5  0.016   0.046   0.066   0.043   8.764   25.728  36.991  23.828
A7  0.009   0.029   0.029   0.022   4.924   16.348  16.364  12.545
A7  0.014   0.025   0.029   0.023   7.806   13.747  16.341  12.631

第二,当我在控制台中运行下面的代码(替换 x 和 y)时,使用实际变量 名称(x = lbls;y = m.raw),我得到的图没有任何错误消息。
但是,当我将代码放入 函数(用 x 和 y 替换实际变量名)我收到上面指出的错误消息

# function to plot
dot.pa <- function(mydata, x, y) {
  pa <-  ggplot(data = mydata, (aes(x = reorder(x, y), y))) +
    geom_point() +
    labs(x = "predicts", y = "weights") +
    coord_flip()

  geom.text.size = 10
  theme.size = (10*1.25) # geom.text.size

  pa2 <- pa + theme_minimal() +
    geom_segment(aes(yend=0.00, xend = x)) +
    geom_text(aes(y, label = round(y, 3), 
              family = "mono", size = geom.text.size,
              vjust = -0.5, hjust = 0.9))

  pa3 <- pa2 + 
           theme(text = element_text(family = "mono", size = theme.size), 
                 axis.text.x = element_text(size = theme.size, family = "mono"),
                 axis.text.y = element_text(size = theme.size, family = "mono")) +
           theme(legend.position = "none")
  return(pa3)
}

#plot function
dot.pa(DF, x=lbls, y=m.raw)

【问题讨论】:

  • 如果您使用dot.pa(DF, x=lbls, y=m.raw) 不引用变量名 调用函数,那么R 将期望这些作为对象存在于环境中。如果您将它们作为字符串传递(dot.pa(DF, x="lbls", y="m.raw"),那么您需要在函数内将它们转换为names
  • 你需要做 tidyeval 来引用列名。除此之外,有助于调试的一件事是保持代码最小化。请参阅minimal reproducible exampleminimal 部分。您问题的实际重点只是函数中的几行;剩下的就是让图表看起来不错,包括将图表分配给 3 个不同的变量,所有这些都使实际问题变得混乱

标签: r function ggplot2


【解决方案1】:

一个更简单的函数来显示问题:

make_plot <- function(mydata, x, y) {
  ggplot(mydata, aes(x=x, y=y)) + geom_point()
}

用不带引号的名字打电话。 R 期望 Species 作为对象存在于环境中:

make_plot(iris, Species, Sepal.Width)
Error in FUN(X[[i]], ...) : object 'Species' not found

用字符串调用不会抛出错误,但也不会像我们预期的那样工作,因为ggplot 期望在aes 中没有引用变量:

make_plot(iris, "Species", "Sepal.Width")

使用您想要绘制的实际向量调用:

make_plot(iris, iris$Species, iris$Sepal.Width)

要使用字符串调用,但得到预期的输出,我们需要转换为name,然后使用!!

make_plot2 <- function(mydata, x, y) {
  ggplot(mydata, aes(x=!!as.name(x), y=!!as.name(y))) + geom_point()
}

make_plot2(iris, "Species", "Sepal.Length")

【讨论】:

    【解决方案2】:

    有了 teofil 的回答,并阅读了有关准引用的更多信息,我能够找到解决方案。下面是sn-p的代码改动

    dot.pa <- function(mydata, x, y) {
    
    x <- enquo(x)
    y <- enquo(y)
    
    pa <-  ggplot(data = mydata, (aes(x = reorder(!!x, !!y), !!y))) +
    geom_point() + 
    labs(x = "predicts", y = "weights") + 
    coord_flip()
    
    
    return(pa)
    }
    

    我 enquo'ed x 和 y,然后在函数中取消引用 !!x 和 !!y 的所有实例以使其正常工作而不会出现任何错误。

    【讨论】: