英文:
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 <- 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)
However, I kept getting the error
> Error in enquo(x) : object 'Time' not found
so then I tried aes_string, like this :
plot_testassess <- function(data1,xvar = "Time",yvar = "Zscore"
){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 <- 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 <-
function(data1,
xvar = "Time",
yvar = "Zscore") {
ggplot(data = data1, aes(.data[[xvar]], .data[[yvar]])) +
geom_line()
}
plot_testassess(MYdata1, "Time", "Zscore")
<!-- -->
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 <-
function(data1,
xvar = "Time",
yvar = "Zscore") {
ggplot(data = data1, aes(
{{xvar}},
{{yvar}}
)) +
geom_line()
}
plot_testassess_nse(MYdata1, Time, Zscore)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论