我需要从几个不同的源数据帧绘制大约 30 个 ggplot2 图,因此我编写了一个函数来创建图,并编写了一个 params 数据帧来存储绘图函数参数。当将参数从 params 数据帧传递到函数时,我收到此错误消息:
data
必须是 fortify()
强制转换的对象,而不是字符串“df1”
这是我的代码,我很感激任何建议:
#load libraries
library(tidyverse)
library(ggthemes)
#set up example dataframe:
df1 <- tibble(
date = seq(ymd('2023-01-01'), ymd('2023-06-30'), by='1 month'),
A = runif(6, 1, 20),
B = runif(6, 1, 20),
C = runif(6, 1, 20)
)
#function to create the plots
gen_plots <- function(df, x, y, title){
df |>
ggplot(
mapping = aes(x = {{x}}, y = {{y}})
) +
geom_line() +
geom_point() +
#theme_economist() +
labs(title = {{title}},
y = "$/MWh",
x = NULL)
}
#test the function--it works:
test_plot <- gen_plots(df1, date, A, "Segment A")
#dataframe with plot parameters (source dataframe, x column, y column, & title):
params <- data.frame(
df = c("df1", "df1", "df1"),
x = c("date", "date", "date"),
y = c("A", "B", "C"),
title = c("Segment A", "Segment B", "Segment C")
)
#desired output is a list containing three plots
#this doesn't work
plots <- pmap(params, gen_plots)
library(tidyverse)
# set up example dataframe:
df1 <- tibble(
date = seq(ymd("2023-01-01"), ymd("2023-06-30"), by = "1 month"),
A = runif(6, 1, 20),
B = runif(6, 1, 20),
C = runif(6, 1, 20)
)
# function to create the plots
gen_plots <- function(df, x, y, title) {
df |>
ggplot(
mapping = aes(x = {{ x }}, y = {{ y }})
) +
geom_line() +
geom_point() +
# theme_economist() +
labs(
title = {{ title }},
y = "$/MWh",
x = NULL
)
}
# test the function--it works:
gen_plots(df1, "date", "B", "Segment B")
#> Warning in geom_line(): All aesthetics have length 1, but the data has 6 rows.
#> ℹ Please consider using `annotate()` or provide this layer with data containing
#> a single row.
#> Warning in geom_point(): All aesthetics have length 1, but the data has 6 rows.
#> ℹ Please consider using `annotate()` or provide this layer with data containing
#> a single row.
# params
list(
df <- list(df1, df1, df1),
x <- c("date", "date", "date"),
y <- c("A", "B", "C"),
title <- c("Segment A", "Segment B", "Segment C")
)
#> [[1]]
#> [[1]][[1]]
#> # A tibble: 6 × 4
#> date A B C
#> <date> <dbl> <dbl> <dbl>
#> 1 2023-01-01 1.59 1.16 14.1
#> 2 2023-02-01 16.5 7.81 8.38
#> 3 2023-03-01 8.41 18.3 14.4
#> 4 2023-04-01 17.8 16.0 14.0
#> 5 2023-05-01 6.93 9.07 8.08
#> 6 2023-06-01 15.8 4.27 9.87
#>
#> [[1]][[2]]
#> # A tibble: 6 × 4
#> date A B C
#> <date> <dbl> <dbl> <dbl>
#> 1 2023-01-01 1.59 1.16 14.1
#> 2 2023-02-01 16.5 7.81 8.38
#> 3 2023-03-01 8.41 18.3 14.4
#> 4 2023-04-01 17.8 16.0 14.0
#> 5 2023-05-01 6.93 9.07 8.08
#> 6 2023-06-01 15.8 4.27 9.87
#>
#> [[1]][[3]]
#> # A tibble: 6 × 4
#> date A B C
#> <date> <dbl> <dbl> <dbl>
#> 1 2023-01-01 1.59 1.16 14.1
#> 2 2023-02-01 16.5 7.81 8.38
#> 3 2023-03-01 8.41 18.3 14.4
#> 4 2023-04-01 17.8 16.0 14.0
#> 5 2023-05-01 6.93 9.07 8.08
#> 6 2023-06-01 15.8 4.27 9.87
#>
#>
#> [[2]]
#> [1] "date" "date" "date"
#>
#> [[3]]
#> [1] "A" "B" "C"
#>
#> [[4]]
#> [1] "Segment A" "Segment B" "Segment C"
pmap(list(df, x, y, title), gen_plots)
#> [[1]]
#> Warning in geom_line(): All aesthetics have length 1, but the data has 6 rows.
#> ℹ Please consider using `annotate()` or provide this layer with data containing
#> a single row.
#> All aesthetics have length 1, but the data has 6 rows.
#> ℹ Please consider using `annotate()` or provide this layer with data containing
#> a single row.
#>
#> [[2]]
#> Warning in geom_line(): All aesthetics have length 1, but the data has 6 rows.
#> ℹ Please consider using `annotate()` or provide this layer with data containing
#> a single row.
#> All aesthetics have length 1, but the data has 6 rows.
#> ℹ Please consider using `annotate()` or provide this layer with data containing
#> a single row.
#>
#> [[3]]
#> Warning in geom_line(): All aesthetics have length 1, but the data has 6 rows.
#> ℹ Please consider using `annotate()` or provide this layer with data containing
#> a single row.
#> All aesthetics have length 1, but the data has 6 rows.
#> ℹ Please consider using `annotate()` or provide this layer with data containing
#> a single row.
创建于 2024-05-06,使用 reprex v2.1.0