gpt4 book ai didi

r - 使用 dplyr 进行探索性绘图

转载 作者:行者123 更新时间:2023-12-04 11:09:09 24 4
gpt4 key购买 nike

我经常使用 d_ply 来制作探索图。

一个简单的例子:

require(plyr)

plot_species <- function(species_data){
p <- qplot(data=species_data,
x=Sepal.Length,
y=Sepal.Width)
print(p)

}

d_ply(.data=iris,
.variables="Species",
function(x)plot_species(x))

这会产生三个独立的地块,每个物种一个。

我想使用 dplyr 中的函数重现这种行为。

这似乎需要在 summarise 调用的函数内重新组装 data.frame,这通常是不切实际的。
require(dplyr)

iris_by_species <- group_by(iris,Species)

plot_species <- function(Sepal.Length,Sepal.Width){

species_data <- data.frame(Sepal.Length,Sepal.Width)

p <- qplot(data=species_data,
x=Sepal.Length,
y=Sepal.Width)
print(p)

}


summarise(iris_by_species, plot_species(Sepal.Length,Sepal.Width))

data.frame 的部分可以直接传递给summary 调用的函数,而不是传递列吗?

最佳答案

我相信你可以和do一起工作使用您在 d_ply 中使用的相同功能完成此任务.它会直接打印到绘图窗口,但也会将绘图保存为 list在由此产生的 data.frame 内如果您使用命名参数(参见帮助页面,这本质上就像使用 dlply )。我没有完全理解所有这些 do可以,但如果我不使用命名参数,我会收到一条错误消息,但绘图仍会打印到绘图窗口(在 RStudio 中)。

plot_species <- function(species_data){
p <- qplot(data=species_data,
x=Sepal.Length,
y=Sepal.Width)
print(p)

}

group_by(iris, Species) %>%
do(plot = plot_species(.))

关于r - 使用 dplyr 进行探索性绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26043441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com