使用d_plyr进行多次绘图操作

时间:2017-03-15 00:49:17

标签: r plot dplyr plyr

我发现了plyr并且正在玩一个例子但是无法理解为什么它不起作用:我有一个10(x,y)坐标的数据框,并希望一个接一个地绘制这些点

## Creating the data
df <- data.frame(a=rnorm(10),b=rnorm(10))
## Empty plot
plot(0, xlim=c(-2,2), ylim=c(-2,2))
## Function to be repeated
plot.pts <- function(x){
points(x$a,x$b)
}
## Magic d_plyr
d_ply(df,plot.pts)

但是我收到了错误

Error in UseMethod("as.quoted") : 
no applicable method for 'as.quoted' applied to an object of class "function"

我明白d_ply是在这种情况下使用的函数,因此我做错了什么?

1 个答案:

答案 0 :(得分:2)

因为你没有真正根据变量将数据框划分为组,只是为每一行调用一个函数,我认为a_plyd_ply更适合:

a_ply(df,.margins = 1, .fun = plot.pts)

在您原来的d_ply调用中,您传递了函数,其中.variables参数告诉d_ply如何对数据进行分组,并给出了该错误。

相关问题