用变量号解释aes中的x和y

时间:2023-01-15 01:29:30

I need to draw a scatterplot with addressing variables by their column numbers instead of names, i.e. instead of ggplot(dat, aes(x=Var1, y=Var2)) I need something like ggplot(dat, aes(x=dat[,1], y=dat[,2])). (I say 'something' because the latter doesn't work).

我需要通过列号而不是名称来绘制带有寻址变量的散点图,即代替ggplot(dat,aes(x = Var1,y = Var2))我需要像ggplot(dat,aes(x = dat [, 1],y = dat [,2]))。 (我说'有些',因为后者不起作用)。

Here is my code:

这是我的代码:

showplot1<-function(indata, inx, iny){
  dat<-indata
  print(nrow(dat)); # this is just to show that object 'dat' is defined
  p <- ggplot(dat, aes(x=dat[,inx], y=dat[,iny]))
  p + geom_point(size=4, alpha = 0.5)
}

testdata<-data.frame(v1=rnorm(100), v2=rnorm(100), v3=rnorm(100), v4=rnorm(100), v5=rnorm(100))
showplot1(indata=testdata, inx=2, iny=3)
# Error in eval(expr, envir, enclos) : object 'dat' not found

4 个解决方案

#1


13  

I strongly suggest using aes_q instead of passing vectors to aes (@Arun's answer). It may look a bit more complicated, but it is more flexible, when e.g. updating the data.

我强烈建议使用aes_q而不是将向量传递给aes(@ Arun的回答)。它可能看起来有点复杂,但它更灵活,例如更新数据。

showplot1 <- function(indata, inx, iny){
  p <- ggplot(indata, 
              aes_q(x = as.name(names(indata)[inx]), 
                    y = as.name(names(indata)[iny])))
  p + geom_point(size=4, alpha = 0.5)
}

And here's the reason why it is preferable:

这就是为什么它更可取的原因:

# test data (using non-standard names)
testdata<-data.frame(v1=rnorm(100), v2=rnorm(100), v3=rnorm(100), v4=rnorm(100), v5=rnorm(100))
names(testdata) <- c("a-b", "c-d", "e-f", "g-h", "i-j")
testdata2 <- data.frame(v1=rnorm(100), v2=rnorm(100), v3=rnorm(100), v4=rnorm(100), v5=rnorm(100))
names(testdata2) <- c("a-b", "c-d", "e-f", "g-h", "i-j")

# works
showplot1(indata=testdata, inx=2, iny=3)
# this update works in the aes_q version
showplot1(indata=testdata, inx=2, iny=3) %+% testdata2

Note: As of ggplot2 v2.0.0 aes_q() has been replaced with aes_() to be consistent with SE versions of NSE functions in other packages.

注意:从ggplot2 v2.0.0开始,aes_q()已替换为aes_(),以与其他软件包中的SEE版本的NSE函数一致。

#2


20  

Your problem is that aes doesn't know your function's environment and it only looks within global environment. So, the variable dat declared within the function is not visible to ggplot2's aes function unless you pass it explicitly as:

您的问题是,aes不了解您的功能环境,只能在全球环境中查看。因此,函数中声明的变量dat对ggplot2的aes函数是不可见的,除非你明确地将它传递给:

showplot1<-function(indata, inx, iny) {
    dat <- indata
    p <- ggplot(dat, aes(x=dat[,inx], y=dat[,iny]), environment = environment())
    p <- p + geom_point(size=4, alpha = 0.5)
    print(p)
}

Note the argument environment = environment() inside the ggplot() command. It should work now.

请注意ggplot()命令中的参数environment = environment()。它现在应该工作。

#3


13  

Try:

尝试:

showplot1 <- function(indata, inx, iny) {
    x <- names(indata)[inx] 
    y <- names(indata)[iny] 
    p <- ggplot(indata, aes_string(x = x, y = y))
    p + geom_point(size=4, alpha = 0.5)
}

Edited to show what's happening - aes_string uses quoted arguments, names gets them using your numbers.

编辑以显示正在发生的事情 - aes_string使用引用的参数,名称使用您的数字获取它们。

#4


0  

provisional solution I found for the moment:

我暂时找到的临时解决方案:

showplot1<-function(indata, inx, iny){
  dat<-data.frame(myX=indata[,inx], myY=indata[,iny])
  print(nrow(dat)); # this is just to show that object 'dat' is defined
  p <- ggplot(dat, aes(x=myX, y=myY))
  p + geom_point(size=4, alpha = 0.5)
}

But I don't really like it because in my real code, I need other columns from indata and here I will have to define all of them explicitly in dat<-...

但我真的不喜欢它,因为在我的真实代码中,我需要来自indata的其他列,在这里我将必须在dat <-..中明确定义所有这些列。

#1


13  

I strongly suggest using aes_q instead of passing vectors to aes (@Arun's answer). It may look a bit more complicated, but it is more flexible, when e.g. updating the data.

我强烈建议使用aes_q而不是将向量传递给aes(@ Arun的回答)。它可能看起来有点复杂,但它更灵活,例如更新数据。

showplot1 <- function(indata, inx, iny){
  p <- ggplot(indata, 
              aes_q(x = as.name(names(indata)[inx]), 
                    y = as.name(names(indata)[iny])))
  p + geom_point(size=4, alpha = 0.5)
}

And here's the reason why it is preferable:

这就是为什么它更可取的原因:

# test data (using non-standard names)
testdata<-data.frame(v1=rnorm(100), v2=rnorm(100), v3=rnorm(100), v4=rnorm(100), v5=rnorm(100))
names(testdata) <- c("a-b", "c-d", "e-f", "g-h", "i-j")
testdata2 <- data.frame(v1=rnorm(100), v2=rnorm(100), v3=rnorm(100), v4=rnorm(100), v5=rnorm(100))
names(testdata2) <- c("a-b", "c-d", "e-f", "g-h", "i-j")

# works
showplot1(indata=testdata, inx=2, iny=3)
# this update works in the aes_q version
showplot1(indata=testdata, inx=2, iny=3) %+% testdata2

Note: As of ggplot2 v2.0.0 aes_q() has been replaced with aes_() to be consistent with SE versions of NSE functions in other packages.

注意:从ggplot2 v2.0.0开始,aes_q()已替换为aes_(),以与其他软件包中的SEE版本的NSE函数一致。

#2


20  

Your problem is that aes doesn't know your function's environment and it only looks within global environment. So, the variable dat declared within the function is not visible to ggplot2's aes function unless you pass it explicitly as:

您的问题是,aes不了解您的功能环境,只能在全球环境中查看。因此,函数中声明的变量dat对ggplot2的aes函数是不可见的,除非你明确地将它传递给:

showplot1<-function(indata, inx, iny) {
    dat <- indata
    p <- ggplot(dat, aes(x=dat[,inx], y=dat[,iny]), environment = environment())
    p <- p + geom_point(size=4, alpha = 0.5)
    print(p)
}

Note the argument environment = environment() inside the ggplot() command. It should work now.

请注意ggplot()命令中的参数environment = environment()。它现在应该工作。

#3


13  

Try:

尝试:

showplot1 <- function(indata, inx, iny) {
    x <- names(indata)[inx] 
    y <- names(indata)[iny] 
    p <- ggplot(indata, aes_string(x = x, y = y))
    p + geom_point(size=4, alpha = 0.5)
}

Edited to show what's happening - aes_string uses quoted arguments, names gets them using your numbers.

编辑以显示正在发生的事情 - aes_string使用引用的参数,名称使用您的数字获取它们。

#4


0  

provisional solution I found for the moment:

我暂时找到的临时解决方案:

showplot1<-function(indata, inx, iny){
  dat<-data.frame(myX=indata[,inx], myY=indata[,iny])
  print(nrow(dat)); # this is just to show that object 'dat' is defined
  p <- ggplot(dat, aes(x=myX, y=myY))
  p + geom_point(size=4, alpha = 0.5)
}

But I don't really like it because in my real code, I need other columns from indata and here I will have to define all of them explicitly in dat<-...

但我真的不喜欢它,因为在我的真实代码中,我需要来自indata的其他列,在这里我将必须在dat <-..中明确定义所有这些列。