如何使用参数列表过滤数据以生成多个数据框和图形

如何使用参数列表过滤数据以生成多个数据框和图形

本文介绍了R - 如何使用参数列表过滤数据以生成多个数据框和图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用过滤器参数列表来生成不同对象的方法.我有一个数据集,我想为其制作多个图表.但是,我希望所有这些图表都基于数据集的子集.出于说明目的,我制作了以下数据.

I am looking for a way to use a list of filter arguments to produce different objects. I have a data set for which I want to make several graphs. However, I want all these graphs based on subsets of the dataset. For illustrative purposes I have made the following data.

df <- data.frame(type = c("b1", "b2", "b1", "b2"),
                 yield = c("15", "10", "5", "0"),
                 temperature = c("2", "21", "26", "13"),
                 Season = c("Winter", "Summer", "Summer", "Autumn"),
                 profit = c(TRUE, TRUE, FALSE, FALSE))

另外,我有一个过滤参数列表.

Also, I have a list of filter arguments.

filters <- c("brand=='b1'",
             "profit",
             "Season=='Summer'",
             "profit==FALSE",
             "yield >= 10",
             "")

我想要的是我可以使用 for 循环让所有这些过滤器生成带有过滤数据的对象,然后绘制图形.我已经通过以下方式尝试过了.

What I would want is that I could use a for loop to have all these filters produce objects with the filtered data, and subsequently plot graphs. I have tried this in the following way.

for(i in 1:length(filters)){
  assign(paste0("df", i), filter(df, factor(filters[i])))
  assign(paste0("plot", i), ggplot(database, aes(x = temperature, y = yield)) + geom_point())
}

然而,这不起作用,因为 filter() 函数不接受 作为参数,也不接受 代码>(例如,"brand=='b1'").我想要的是 brand=='b1',所以 filter() 接受它作为参数.有没有人有这样做的想法?

However, this did not work because the filter() function does not accept <fct> as an argument, nor <chr> (e.g., "brand=='b1'"). What I would want is brand=='b1', so filter() accepts it as an argument. Does anybody have an idea to do this?

另外,作为一个附加问题,我想自动化整个过程并以组合图结束,所以 grid.arrange() 最后.当然,我可以使用 length(filters) 的一些部分来自动化 ncolnrow.但是如何在 grid.arrange() 中获取所有生成的图?这应该在 for 循环之外,对吧?这里有什么想法吗?

Also, as an additional question, I would like to automate the whole process and end with an combined graph, so grid.arrange() at the end. Of course I could automate the ncol and nrow with some devision of length(filters). But how to I get all the produced plots in the grid.arrange()? This should probably be outside the for loop, right? Any ideas here?

推荐答案

您可以使用 evalparse 来完成.

You can do it by using eval and parse.

此外,在自定义函数上使用 lapply 听起来比带有 assignfor 循环更合理.结果是 ggplot 对象的列表.

Also, a lapply over a custom function sounds more reasonable than a for loop with assign. The result is a list of ggplot objects.

gridExtra 包中将所有图表设置在一起 grid.arrange 工作正常.您只需要将图表列表分配给名为 grobs 的参数.

To set all charts all together grid.arrange from the gridExtra package works fine. You just need to assign the list of your charts to the argument called grobs.

library(dplyr)
library(ggplot2)

df <- data.frame(type = c("b1", "b2", "b1", "b2"),
                 yield = c(15, 10, 5, 0),
                 temperature = c("2", "21", "26", "13"),
                 Season = c("Winter", "Summer", "Summer", "Autumn"),
                 profit = c(TRUE, TRUE, FALSE, FALSE))

filters <- list("type=='b1'",
                "profit",
                "Season=='Summer'",
                "profit==FALSE",
                "yield >= 10",
                "TRUE")


myfun <- function(fltr, df){

  df <- filter(df, eval(parse(text = fltr)))
  ggplot(df, aes(x = temperature, y = yield)) + geom_point()

}


ggs <- lapply(filters, myfun, df = df)

gridExtra::grid.arrange(grobs = ggs)

我对您的数据进行了一些更改:yield 必须是数字,因为您使用的过滤器仅适用于数字向量,而最后一个过滤器(为空)现在等于TRUE"[我想您想要考虑一切]

I made a couple of changes in your data: yield must be a numeric since you are using a filter applicable only to numeric vectors and the last filter (which was empty) is now equal to "TRUE" [I supposed you wanted to take everything in consideration]

这篇关于R - 如何使用参数列表过滤数据以生成多个数据框和图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 03:21