Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2无法识别函数调用中的列_R_Function_Ggplot2 - Fatal编程技术网

R ggplot2无法识别函数调用中的列

R ggplot2无法识别函数调用中的列,r,function,ggplot2,R,Function,Ggplot2,当我在控制台中运行时,我的代码运行良好,并绘制了图形。但是,当我将其放入函数中时,会收到一条错误消息,指示找不到其中一列 "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.

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

"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显示问题的简单函数:

make_plot <- function(mydata, x, y) {
  ggplot(mydata, aes(x=x, y=y)) + geom_point()
}
使用字符串调用不会引发错误,但也不会像我们预期的那样工作,因为
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")

make_plot2有了teofil的答案,并阅读了更多关于准旋转的内容,我就能够得到一个解决方案。下面是代码更改的片段

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)
}

dot.pa如果使用
dot.pa(DF,x=lbls,y=m.raw)
调用函数而不引用变量名,则
R
将期望这些变量作为对象存在于环境中。如果您将它们作为字符串传递(
dot.pa(DF,x=“lbls”,y=“m.raw”)
,然后您需要将它们转换为函数中的
名称。可能的重复项需要执行tidyeval以引用列名。除此之外,有助于调试的一件事是将代码保持最小。请参阅的最小部分。问题的实际焦点仅是函数中的几行;t他其余的工作是使图表看起来很漂亮,包括将图表分配给3个不同的变量,所有这些都混淆了实际问题
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)
}