ggplot2 在函数中无法识别列名

huangapple go评论64阅读模式
英文:

ggplot2 not recognising column names in function

问题

我的目标是创建一个函数,该函数可以输入特定的列名和数据框名称,以便以一致的格式输出图形。然而,我在使用ggplot2创建函数时遇到了一些问题,因为出现了列名识别的问题。到目前为止,我还没有成功使用其他问题中提出的任何修复方法。

以一个测试数据集为例,这是我首先尝试的:

set.seed(123)

MYdata1 <- data.frame(Time = 1:1000,
                    Zscore = rnorm(1000))

plot_testassess <- function(data1, xvar = "Time", yvar = "Zscore") {
    ggplot(data = data1, aes(.data[[xvar]], .data[[yvar]]))
}

plot_testassess(MYdata1, Time, Zscore)

然而,我一直收到以下错误:

Error in enquo(x) : object 'Time' not found

所以我尝试了aes_string,像这样:

plot_testassess <- function(data1, xvar = "Time", yvar = "Zscore") {
    ggplot(data = data1, aes_string(x = xvar, y = yvar))
}

plot_testassess(MYdata1, "Time", "Zscore")

但在这种情况下,我收到以下错误:

Error in aes_string(x = xvar, y = yvar) : object 'Time' not found

我不知道如何让ggplot2识别我的列变量不是对象。有人有什么想法吗?

英文:

My goal is to have a function in which can input specific column and df names, in order to output graphs in a consistent format. However, I am having some trouble with creating functions using ggplot2, because of problems with column name recognition. So far, I haven't had any success with any of the fixes people have proposed in other questions addressing this issue.

Using a test dataset as an example, this is what I tried first

set.seed(123)

MYdata1 &lt;- data.frame(Time = 1:1000,
                    Zscore = rnorm(1000))

plot_testassess &lt;- function(data1,xvar = &quot;Time&quot;,yvar = &quot;Zscore&quot;
){ggplot(data = data1, aes(.data[[xvar]], .data[[yvar]]))}

plot_testassess(MYdata1,Time,Zscore)

However, I kept getting the error

> Error in enquo(x) : object 'Time' not found

so then I tried aes_string, like this :

plot_testassess &lt;- function(data1,xvar = &quot;Time&quot;,yvar = &quot;Zscore&quot;
){ggplot(data = data1, aes_string(x=xvar,y=yvar))}

plot_testassess(MYdata1,Time,Zscore)

But in this case I got the following error.

> Error in aes_string(x = xvar, y = yvar) : object 'Time' not found

I am at a loss as to how to make ggplot2 recognise that my column variables are not objects. Does anyone have any thoughts?

答案1

得分: 2

library(ggplot2)

set.seed(123)
MYdata1 <- data.frame(Time = 1:1000,
                      Zscore = rnorm(1000))
plot_testassess <-
  function(data1,
           xvar = "Time",
           yvar = "Zscore") {
    ggplot(data = data1, aes(.data[[xvar]], .data[[yvar]])) +
      geom_line()
  }

plot_testassess(MYdata1, "Time", "Zscore")
plot_testassess_nse <-
  function(data1,
           xvar = "Time",
           yvar = "Zscore") {
    ggplot(data = data1, aes(
        {{xvar}}, 
        {{yvar}}
      )) +
      geom_line()
  }

plot_testassess_nse(MYdata1, Time, Zscore)
英文:
library(ggplot2)

set.seed(123)
MYdata1 &lt;- data.frame(Time = 1:1000,
                      Zscore = rnorm(1000))

You can make your function work, by putting quotation marks around the
variable names in the function call. (I added geom_line() to you function
so we get a visible result; nothing else is changed here.)

plot_testassess &lt;-
  function(data1,
           xvar = &quot;Time&quot;,
           yvar = &quot;Zscore&quot;) {
    ggplot(data = data1, aes(.data[[xvar]], .data[[yvar]])) +
      geom_line()
  }

plot_testassess(MYdata1, &quot;Time&quot;, &quot;Zscore&quot;)

ggplot2 在函数中无法识别列名<!-- -->

If you prefer to be able to provide the variable names without quotation
marks you can drop the .data[[]] and embrace xvar and yvar with {{}}.

plot_testassess_nse &lt;-
  function(data1,
           xvar = &quot;Time&quot;,
           yvar = &quot;Zscore&quot;) {
    ggplot(data = data1, aes(
        {{xvar}}, 
        {{yvar}}
      )) +
      geom_line()
  }

plot_testassess_nse(MYdata1, Time, Zscore)

ggplot2 在函数中无法识别列名<!-- -->

huangapple
  • 本文由 发表于 2023年7月11日 00:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定