1

With the amazing help of @Tung we have created a function that creates a list of ggplots through a loop using purrr::pwalk. However, the ploblem is that the plots are printed automatically and it is not possible (or I am not able to solve the problem) to save them as a list of plots. I am coming from this post: Passing labels to xlab and ylab in ggplot2

NOTE: I need to change the ylab and xlab labels from each plot.

The function to plot is as follows:

library(tidyverse)

plot_scatter_with_label <- function(dat,
                                    var_x,
                                    var_y,
                                    label_x,
                                    label_y,
                                    geom_smooth = FALSE,
                                    point_shape = 16,
                                    point_color = "#EB3300",
                                    point_size = 1,
                                    point_alpha = 1,
                                    smooth_method = "loess",
                                    smooth_se = FALSE,
                                    smooth_color = "navy") {

  if (is.character(var_x)) {
    print('character column names supplied, use rlang::sym()')
    var_x <- rlang::sym(var_x)
  } else {
    print('bare column names supplied, use dplyr::enquo()')
    var_x <- enquo(var_x)
  }

  if (is.character(var_y)) {
    var_y <- rlang::sym(var_y)
  } else {
    var_y <- enquo(var_y)
  }

  p <- ggplot(dat, aes(x = !! var_x, y = !! var_y)) + 
    geom_point(shape = point_shape, color = point_color, 
               size = point_size, alpha = point_alpha) + 
    ylab(label_y) + 
    xlab(label_x) +
    ggtitle(paste0(label_x, " ~ ", label_y))
  print(p)

}

Create a data frame so that we can loop through every row and column

var_y = c("mpg", "hp")
label_y = c("Miles per gallon [Mpg]", "Horse power [CV]")
var_x = c("cyl", "gear")
label_x = c("Cylinders [n]", "Gear [n]")

var_xy <- expand.grid(var_x, var_y, stringsAsFactors = FALSE)
label_xy <- expand.grid(label_x, label_y, stringsAsFactors = FALSE)
select_dat <- data.frame(var_xy, label_xy, stringsAsFactors = FALSE)

pwalk(select_dat, ~ plot_scatter_with_label(mtcars, ..1, ..2,..3,..4))

The problem is that using pwalk and I am guessing that due to the print(p) from the function plot_scatter_with_label, the plots are automatically displayed. Instead, I would like to save them in a list of plots. For example: I would like:

p_list = pwalk(select_dat, ~ plot_scatter_with_label(mtcars, ..1, ..2,..3,..4)) 

where p_list is a list of plots to "play with" using some function to arrange them like

cowplot::plot_grid(plotlist=p_list, nrow=3,ncol=2)

or

ggpubr::ggarrange(plotlist=p_list, nrow=3,ncol=2)

@Tung has recomended me to have a look at this post: Multiple plots in for loop ignoring par

However, I am still unable to find the solution.

Any help will he highly appreciated.

Thanks a lot in advance,

Best regards,

Juan Antonio

Edit:: corrected spelling of function plotlist

Ryan John
  • 1,410
  • 1
  • 15
  • 23

1 Answers1

1

The pwalk function is explicitly designed not to return an output, but instead to focus on side-effects (like printing, reading/writing, or plotting). It is an alternative function to pmap, which does return its output.

You could return the plots in a list like this:

  1. Change the last line of your custom function from print(p) to return(p)
  2. Use the pmap function rather than pwalk
A. Stam
  • 2,148
  • 14
  • 29