使用 R 创建自定义绘图函数并使用动态参数 运行

Create customized plotting function and run it with dynamic parameters using R

编辑代码,例如 this link,我希望将其转换为自定义函数,并可以轻松地将其应用于具有类似格式的新数据帧和参数:

library(ggplot2)
library(patchwork)

# Create ggplot2 scatterplot
p1 <- ggplot(iris,               
               aes(x = Sepal.Length,
                   y = Sepal.Width,
                   col = Species)) +
  geom_point()
p1
# Create ggplot2 barchart
p2 <- ggplot(iris,               
               aes(x = Species,
                   y = Sepal.Width,
                   fill = Species)) +
  geom_bar(stat = "identity")
p2

# Create ggplot2 boxplot
p3 <- ggplot(iris,
               aes(x = Species,
                   y = Sepal.Width,
                   col = Species)) +
  geom_boxplot()
p3

# Create plot composition
p <- (p1 + p2) / p3
# Draw plot composition
p

输出:

使用下面的代码,我创建了 3 个示例,为 df1df2、[= 设置了 p1p2p3 的参数18=]分别为:

set.seed(1)

df1 <- slice_sample(iris, n = 10)
df2 <- slice_sample(iris, n = 10)
df3 <- slice_sample(iris, n = 10)

# to set df1's and arguments (seems not good enough to store parameters, maybe I need to save it as config or ymal file?)
df1_plot1_param <- c(df='df1', x='Petal.Width', y='Sepal.Width', col='Species')
df1_plot2_param <- c(df='df1', x='Petal.Length', y='Sepal.Width', fill='Species')
df1_plot3_param <- c(df='df1', x='Sepal.Width', y='Petal.Width', col='Species')

# to set df2's arguments
df2_plot1_param <- c(df='df2', x='Sepal.Length', y='Sepal.Width', col='Species')
df2_plot2_param <- c(df='df2', x='Petal.Length', y='Sepal.Width', fill='Species')
df2_plot3_param <- c(df='df2', x='Sepal.Length', y='Petal.Width', col='Species')

# to set df3's arguments
df3_plot1_param <- c(df='df3', x='Sepal.Length', y='Sepal.Width', col='Species')
df3_plot2_param <- c(df='df3', x='Petal.Length', y='Sepal.Width', fill='Species')
df3_plot3_param <- c(df='df3', x='Sepal.Length', y='Petal.Width', col='Species')

for i in c(df1's params, df2's params, df3's params):
  purrr:map(plot_func(df{i}_param))

最终结果将是 df 秒的 3 个图,参数已预定义。

但我不知道如何使整个代码运行,感谢您的提前帮助,非常感谢。

参考链接:

Creating multiple ggplots with dplyr

尝试这样的事情。您可以在函数中指定 X/Y 和分组等。

编辑:添加数据框作为每个问题函数的参数

set.seed(1)

df1 <- slice_sample(iris, n = 10)


library(ggplot2)
library(patchwork)
    
my_plot = function(DF=df, X="", Y="", GROUP=""){
  
  # Create ggplot2 scatterplot
  p1 <- ggplot(DF,               
               aes(x = .data[[X]],
                   y = .data[[Y]],
                   col = Species)) +
    geom_point()
  p1
  # Create ggplot2 barchart
  p2 <- ggplot(DF,               
               aes(x = .data[[GROUP]],
                   y = .data[[Y]],
                   fill = .data[[GROUP]])) +
    geom_bar(stat = "identity")
  p2
  
  # Create ggplot2 boxplot
  p3 <- ggplot(DF,
               aes(x = .data[[GROUP]],
                   y = .data[[Y]],
                   col = .data[[GROUP]])) +
    geom_boxplot()
  p3
  
  # Create plot composition
  p <- (p1 + p2) / p3
  # Draw plot composition
  return(p)
  
}

test = my_plot(DF=iris, X= "Sepal.Length",Y = "Sepal.Width", GROUP="Species")