Passing labels to xlab and ylab in ggplot2

╄→гoц情女王★ 提交于 2019-12-20 02:45:21

问题


I have created a function where the objective is to create a series of plots in a vectorzation way. The functions partially does what I want which is update update the plot based on the selected variables. However, I am not able to pass the label argument (i.e. label_x and label_y) so that the xlab and ylab are updated consistently.

library(tidyverse)
plot_scatter_with_label <- function(df,
                                    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") {
  df <- data.frame(lapply(df, function(x) as.numeric(as.character(x))))
  scatter_plot <- function(x, y) {
    p <- ggplot(df, aes_string(x = x, y = y)) + 
      geom_point(shape = point_shape, color = point_color, size = point_size, alpha = point_alpha) + 
      ylab(label_y) + xlab(label_x)
    p
  }
  map2(
    var_y, label_y,
    ~ map(var_x, scatter_plot, y = .x)
  )
}

Example

plot_scatter_with_label(
  df = mtcars,
  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]")
)

I was expecting to obtain the following plots:

1) mpg vs cyl

2) mpg vs gear

3) hp vs cyl

4) hp vs gear

It appears that I got these 4 plots but the labels are not updated as expected. It always returns the fist argument of defined in label_x and label_y.

Any help would be highly appreciated.

Best regards,


回答1:


We can use pmap or pwalk to pass data to plot_scatter_with_label function

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)
str(select_dat)

#> 'data.frame':    4 obs. of  4 variables:
#>  $ Var1  : chr  "cyl" "gear" "cyl" "gear"
#>  $ Var2  : chr  "mpg" "mpg" "hp" "hp"
#>  $ Var1.1: chr  "Cylinders [n]" "Gear [n]" "Cylinders [n]" "Gear [n]"
#>  $ Var2.1: chr  "Miles per gallon [Mpg]" "Miles per gallon [Mpg]" "Horse power [CV]" "Horse power [CV]"

Pass each row to plot_scatter_with_label function

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

#> [1] "character column names supplied, use rlang::sym()"

#> [1] "character column names supplied, use rlang::sym()"

#> [1] "character column names supplied, use rlang::sym()"

#> [1] "character column names supplied, use rlang::sym()"

Created on 2019-02-14 by the reprex package (v0.2.1.9000)



来源:https://stackoverflow.com/questions/54664092/passing-labels-to-xlab-and-ylab-in-ggplot2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!