将列表中的数据帧发送到绘图函数

时间:2021-10-28 22:56:23

I'm trying to make multiple ggplot charts from multiple data frames. I have developed the code below but the final loop doesn't work.

我正在尝试从多个数据帧中制作多个ggplot图表。我已经开发了下面的代码,但最后一个循环不起作用。

df1 <- tibble(
  a = rnorm(10),
  b = rnorm(10)
)

df2 <- tibble(
  a = rnorm(20),
  b = rnorm(20)
)

chart_it <- function(x) {
  x %>% ggplot() +
    geom_line(mapping = aes(y=a,x=b)) +
    ggsave(paste0(substitute(x),".png"))
}

ll <- list(df1,df2)

for (i in seq_along(ll)) {
 chart_it(ll[[i]])
}

I know its something to do with

我知道它与之有关

ll[[i]]

but I dont understand why because when I put that in the console it gives the dataframe I want. Also, is there a way do this the tidyverse way with the map functions instead of a for loop?

但我不明白为什么,因为当我把它放在控制台时,它给出了我想要的数据帧。另外,有没有办法使用map函数而不是for循环来实现tidyverse方式呢?

2 个解决方案

#1


2  

I assume you want to see two files called df1.png and df2.png at the end.

我假设你想在最后看到两个名为df1.png和df2.png的文件。

You need to somehow pass on the names of the dataframes to the function. One way of doing it would be through named list, passing the name along with the content of the list element.

您需要以某种方式将数据帧的名称传递给函数。一种方法是通过命名列表,将名称与list元素的内容一起传递。

library(ggplot2)
library(purrr)

df1 <- tibble(
  a = rnorm(10),
  b = rnorm(10)
)

df2 <- tibble(
  a = rnorm(20),
  b = rnorm(20)
)

chart_it <- function(x, nm) {
  p <- x %>% ggplot() +
    geom_line(mapping = aes(y=a,x=b))
  ggsave(paste0(nm,".png"), p, device = "png")
}

ll <- list(df1=df1,df2=df2)

for (i in seq_along(ll)) {
  chart_it(ll[[i]], names(ll[i]))
}

In tidyverse you could just replace the loop with the following command without modifying the function.

在tidyverse中,您可以使用以下命令替换循环而无需修改函数。

purrr::walk2(ll, names(ll),chart_it)

or simply

或简单地说

purrr::iwalk(ll, chart_it)

There's also imap and lmap, but they will leave some output in the console, which is not what you would like to do, I guess.

还有imap和lmap,但它们会在控制台中留下一些输出,这不是你想做的,我想。

#2


0  

The problem is in your chart_it function. It doesn't return a ggplot. Try saving the result of the pipe into a variable and return() that (or place it as the last statement in the function).

问题出在你的chart_it函数中。它没有返回ggplot。尝试将管道的结果保存到变量中并返回()(或将其作为函数中的最后一个语句)。

Something along the lines of

有点像

chart_it <- function(x) {
  chart <- x %>% ggplot() +
    geom_line(mapping = aes(y=a,x=b))

    ggsave(paste0(substitute(x),".png")) # this will save the last ggplot figure

    return(chart)
}

#1


2  

I assume you want to see two files called df1.png and df2.png at the end.

我假设你想在最后看到两个名为df1.png和df2.png的文件。

You need to somehow pass on the names of the dataframes to the function. One way of doing it would be through named list, passing the name along with the content of the list element.

您需要以某种方式将数据帧的名称传递给函数。一种方法是通过命名列表,将名称与list元素的内容一起传递。

library(ggplot2)
library(purrr)

df1 <- tibble(
  a = rnorm(10),
  b = rnorm(10)
)

df2 <- tibble(
  a = rnorm(20),
  b = rnorm(20)
)

chart_it <- function(x, nm) {
  p <- x %>% ggplot() +
    geom_line(mapping = aes(y=a,x=b))
  ggsave(paste0(nm,".png"), p, device = "png")
}

ll <- list(df1=df1,df2=df2)

for (i in seq_along(ll)) {
  chart_it(ll[[i]], names(ll[i]))
}

In tidyverse you could just replace the loop with the following command without modifying the function.

在tidyverse中,您可以使用以下命令替换循环而无需修改函数。

purrr::walk2(ll, names(ll),chart_it)

or simply

或简单地说

purrr::iwalk(ll, chart_it)

There's also imap and lmap, but they will leave some output in the console, which is not what you would like to do, I guess.

还有imap和lmap,但它们会在控制台中留下一些输出,这不是你想做的,我想。

#2


0  

The problem is in your chart_it function. It doesn't return a ggplot. Try saving the result of the pipe into a variable and return() that (or place it as the last statement in the function).

问题出在你的chart_it函数中。它没有返回ggplot。尝试将管道的结果保存到变量中并返回()(或将其作为函数中的最后一个语句)。

Something along the lines of

有点像

chart_it <- function(x) {
  chart <- x %>% ggplot() +
    geom_line(mapping = aes(y=a,x=b))

    ggsave(paste0(substitute(x),".png")) # this will save the last ggplot figure

    return(chart)
}