通过变量编号来解决aes中的x和y [英] Addressing x and y in aes by variable number

查看:155
本文介绍了通过变量编号来解决aes中的x和y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的代码:

 showplot1< -function(indata,inx,iny){
dat< -indata
print(nrow(dat)); #这只是为了显示对象'dat'被定义为
p < - ggplot(dat,aes(x = dat [,inx],y = dat [,iny]))
p + geom_point(大小= 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)




 #eval中的错误(expr,envir,enclos):找不到对象'dat'



解决方案

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

  showplot1 < - 函数(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)
}

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

 #测试数据(使用非标准名称)
testdata< -data.frame(v1 = rnorm(100 ),v2 = rnorm(100),v3 = rnorm(100),v4 = rnorm(100),v5 = rnorm(100))
名称(testdata)< -c(ab (100),v2 = rnorm(100),v3 = rnorm(100),v4 = rnorm(100),v4 = rnorm(100) 100),v5 = rnorm(100))
名称(testdata2)<-c(ab,cd,ef,gh,ij)

#工程
showplot1(indata = testdata,inx = 2,iny = 3)
#此更新在aes_q版本中工作
showplot1(indata = testdata,inx = 2,iny = 3 )%+%testdata2






注意:自 aes_q()已被替换为 aes_() 与其他软件包中SE版本的NSE函数一致。


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

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

解决方案

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.

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.

这篇关于通过变量编号来解决aes中的x和y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆