ggplot2 在函数中无法识别列名

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

ggplot2 not recognising column names in function

问题

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

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

  1. set.seed(123)
  2. MYdata1 <- data.frame(Time = 1:1000,
  3. Zscore = rnorm(1000))
  4. plot_testassess <- function(data1, xvar = "Time", yvar = "Zscore") {
  5. ggplot(data = data1, aes(.data[[xvar]], .data[[yvar]]))
  6. }
  7. plot_testassess(MYdata1, Time, Zscore)

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

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

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

  1. plot_testassess <- function(data1, xvar = "Time", yvar = "Zscore") {
  2. ggplot(data = data1, aes_string(x = xvar, y = yvar))
  3. }
  4. 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

  1. set.seed(123)
  2. MYdata1 &lt;- data.frame(Time = 1:1000,
  3. Zscore = rnorm(1000))
  4. plot_testassess &lt;- function(data1,xvar = &quot;Time&quot;,yvar = &quot;Zscore&quot;
  5. ){ggplot(data = data1, aes(.data[[xvar]], .data[[yvar]]))}
  6. 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 :

  1. plot_testassess &lt;- function(data1,xvar = &quot;Time&quot;,yvar = &quot;Zscore&quot;
  2. ){ggplot(data = data1, aes_string(x=xvar,y=yvar))}
  3. 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

  1. library(ggplot2)
  2. set.seed(123)
  3. MYdata1 <- data.frame(Time = 1:1000,
  4. Zscore = rnorm(1000))
  1. plot_testassess <-
  2. function(data1,
  3. xvar = "Time",
  4. yvar = "Zscore") {
  5. ggplot(data = data1, aes(.data[[xvar]], .data[[yvar]])) +
  6. geom_line()
  7. }
  8. plot_testassess(MYdata1, "Time", "Zscore")
  1. plot_testassess_nse <-
  2. function(data1,
  3. xvar = "Time",
  4. yvar = "Zscore") {
  5. ggplot(data = data1, aes(
  6. {{xvar}},
  7. {{yvar}}
  8. )) +
  9. geom_line()
  10. }
  11. plot_testassess_nse(MYdata1, Time, Zscore)
英文:
  1. library(ggplot2)
  2. set.seed(123)
  3. MYdata1 &lt;- data.frame(Time = 1:1000,
  4. 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.)

  1. plot_testassess &lt;-
  2. function(data1,
  3. xvar = &quot;Time&quot;,
  4. yvar = &quot;Zscore&quot;) {
  5. ggplot(data = data1, aes(.data[[xvar]], .data[[yvar]])) +
  6. geom_line()
  7. }
  8. 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 {{}}.

  1. plot_testassess_nse &lt;-
  2. function(data1,
  3. xvar = &quot;Time&quot;,
  4. yvar = &quot;Zscore&quot;) {
  5. ggplot(data = data1, aes(
  6. {{xvar}},
  7. {{yvar}}
  8. )) +
  9. geom_line()
  10. }
  11. 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:

确定