将标签传递给ggplot2中的xlab和ylab

时间:2019-02-13 06:43:07

标签: r ggplot2 purrr tidyeval

我创建了一个函数,目标是以矢量化方式创建一系列绘图。这些功能部分地实现了我想要的功能,即根据所选变量更新绘图。但是,我无法传递label参数(即label_x和label_y),从而无法始终更新xlab和ylab。

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)
  )
}

示例

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]")
)

我期望获得以下图:

1)mpg vs cyl

2)mpg vs装备

3)hp vs cyl

4)hp vs gear

看来我得到了这4个图,但是标签没有按预期更新。它总是返回label_x和label_y中定义的fist参数。

任何帮助将不胜感激。

最诚挚的问候

1 个答案:

答案 0 :(得分:1)

我们可以使用<div class="container"> <div id="search_bar"> <input placeholder="Search" type="search"> </div> <div id="search_results"></div> <div id="playlist_reel"> <img src="http://placekitten.com/400/50" alt=""> </div> </div>pmap将数据传递到pwalk函数

plot_scatter_with_label

创建一个数据框,以便我们可以遍历每一行和每一列

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)

}

将每一行传递给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]" 函数

plot_scatter_with_label

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()"

reprex package(v0.2.1.9000)于2019-02-14创建