在R中传递值 [英] Pass by value in R

查看:139
本文介绍了在R中传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图调用grid.arrange在同一个ggplot2图上放置多个图时,我首先创建了我想要的图的列表。然后我构建相应的参数列表来调用grid.arrange,正如解释在前一个问题中。这是我的代码(我的数据框叫做manip):

  args.list<  -  NULL; 
plot.list< - NULL;
for(m in names(manip [2:10])){
plot.list< -c(plot.list,list(qplot(manip $ side,y = manip [,m] ,ylab = m))
}
args.list< - c(plot.list,1,9)
names(args.list)< - c(names(manip) [2:10],list(nrow,ncol))
do.call(grid.arrange,args.list)

除了9个图完全相同之外,它可以工作!检查后,数据总是与 m = 10 。所以我猜测 m 的值不是在循环中赋值的,而是稍后评估的,但是标签 ylab = m 正确分配的,并且对于所有图形都是不同的。

有什么区别,以及解释者如何选择何时评估m的情节。有人可以解释吗? 解决方案

我会先回答您的问题,然后显示一个替代使用方面的阴谋。



编辑



下面的代码看起来很简单:

 <$ c $ (2),
function(x)qplot(manip $ mpg,y = manip [, x

$ name $($)
do.call(grid.arrange,c(plot.list,nrow = 10))






我知道。但是,你是否考虑过使用facet代替你的情节?



以下代码更为简单:执行查询并生成易于解释的图形:



$ p $ lt; code> library(reshape2)
manip< - mtcars
mmanip< - melt(manip,id.vars = mpg)
str(mmanip)
ggplot(mmanip,aes(x = mpg,y = value))+
geom_point(stat =identity)+
facet_grid( 。〜variable,scales =free)


When trying to call grid.arrange to put multiple plots on a same ggplot2 graph, I first build a list of the plots I want. Then I build the corresponding argument list to call grid.arrange, as was explained in a previous question. This is my code (my dataframe is called manip):

args.list <- NULL;
plot.list <- NULL;
for (m in names(manip[2:10])) {
  plot.list <- c(plot.list, list(qplot(manip$side, y=manip[,m],ylab=m))
}
args.list <- c(plot.list, 1, 9)
names(args.list) <- c(names(manip)[2:10], list("nrow","ncol"))
do.call(grid.arrange, args.list)

This works, except that the 9 graphs are exactly the same! After checking, it turns out that the data is always the one corresponding to m=10. So my guess was that the value of m is not assigned in the loop, but evaluated later. However, the label ylab=m is assigned correctly and is different for all the graphs.

So I don't really get what the difference is and how the interpreter chooses when to evaluate m for the plots. Can someone explain?

解决方案

I will first answer your question and then show an alternative using a facet plot.

Edited

The following, much simplified, code seems to work:

library(gridExtra)
manip <- mtcars
plot.list <- lapply(2:11, 
                    function(x)qplot(manip$mpg, y=manip[, x], 
                    ylab=names(manip)[x]))
do.call(grid.arrange, c(plot.list, nrow=10))

It produces this ugly plot:


Without knowing your objectives, it is dangerous to try and give advice, I know. Nonetheless, have you considered using facets for your plot instead?

The following code is much simpler, executes quiker and produces a graph that is easier to interpret:

library(reshape2)
manip <- mtcars
mmanip <- melt(manip, id.vars="mpg")
str(mmanip)
ggplot(mmanip, aes(x=mpg, y=value)) + 
    geom_point(stat="identity") + 
    facet_grid(.~variable, scales="free")

这篇关于在R中传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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