Using dplyr for exploratory plots

一个人想着一个人 提交于 2019-12-21 02:40:48

问题


I regularly used d_ply to produce exploratory plots.

A trivial example:

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

Which produces three separate plots, one for each species.

I would like to reproduce this behaviour using functions in dplyr.

This seems to require the reassembly of the data.frame within the function called by summarise, which is often impractical.

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

Can parts of the data.frame be passed to the function called by summarise directly, rather than passing columns?


回答1:


I believe you can work with do for this task with the same function you used in d_ply. It will print directly to the plotting window, but also saves the plots as a list within the resulting data.frame if you use a named argument (see help page, this is essentially like using dlply). I don't fully grasp all that do can do, but if I don't use a named argument I get an error message but the plots still print to the plotting window (in 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(.))


来源:https://stackoverflow.com/questions/26043441/using-dplyr-for-exploratory-plots

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!