使用 `rlang` 在 `ggplot` 中使用 `ggrepel` 进行条件标记

using `rlang` for conditional labelling in `ggplot` using `ggrepel`

我正在编写一个自定义函数来创建一个带有附加到点的标签的散点图。这是相同内容的最小版本。

# needed libraries
library(tidyverse)
library(ggplot2)
library(ggrepel)

# custom function
label_adder <- function(data, x, y, label.var) {
  # basic plot
  plot <-
    ggplot(data = data,
           mapping = aes(
             x = !!rlang::enquo(x),
             y = !!rlang::enquo(y)
           )) +
    geom_point() +
    geom_smooth(method = "lm")

  # adding label
  plot <-
    plot +
    geom_label_repel(mapping = aes(label = !!rlang::enquo(label.var)))

  return(plot)
}

# creating dataframe
mtcars_new <- mtcars %>%
  tibble::rownames_to_column(., var = "car") %>%
  tibble::as_data_frame(x = .)

# using the function
label_adder(
  data = mtcars_new,
  x = wt,
  y = mpg,
  label.var = car
)

reprex package (v0.2.0.9000) 创建于 2018-08-30。

问题 我似乎无法弄清楚如何使标签以 xy 变量的值为条件。例如,假设用户不想显示散点图中的所有点,而只显示带有 (examples):

的点
wt > 5
wt < 4 & mpg < 20
wt > 4 | mpg > 25

等等

我可以使用 rlanggeom_label_repel 的代码中更改什么,以便用户提供的任何条件(涉及 x and/or y)将被评估并且只有那些标签将显示在图中?

你可以尝试这样的事情。在这里,我向您的函数添加一个表达式参数,检查是否正在使用该表达式,然后进行相应的过滤。

library(tidyverse)
library(ggplot2)
library(ggrepel)

# custom function
label_adder <- function(data, x, y, label.var, exp = NULL) {
  param_list <- as.list(match.call())

  if("exp" %in% names(param_list)){
    plot <-
    ggplot(
           mapping = aes(
             x = !!rlang::enquo(x),
             y = !!rlang::enquo(y)
           )) +
    geom_point(data = data) +
    geom_smooth(data = data, method = "lm")+
    geom_label_repel(data = data %>% filter(!!rlang::enquo(exp)), 
                     mapping = aes(label = !!rlang::enquo(label.var)))
    return(plot)
  }
  else{
    plot <-
    ggplot(data = data,
           mapping = aes(
             x = !!rlang::enquo(x),
             y = !!rlang::enquo(y)
           )) +
    geom_point() +
    geom_smooth(method = "lm")+
    geom_label_repel(mapping = aes(label = !!rlang::enquo(label.var)))

  return(plot)
  }
}

# creating dataframe
mtcars_new <- mtcars %>%
  tibble::rownames_to_column(., var = "car") %>%
  tibble::as_data_frame(x = .)

# using the function
label_adder(
  data = mtcars_new,
  x = wt,
  y = mpg,
  label.var = car
)

label_adder(
  data = mtcars_new,
  x = wt,
  y = mpg,
  label.var = car,
  exp = wt < 4 & mpg < 20
)

reprex 创建于 2018-08-30 包 (v0.2.0).

更新

label_adder <- function(data, x, y, label.var, exp = NULL) {
  param_list <- as.list(match.call())

  if("exp" %in% names(param_list)){
    my_exp <- rlang::enquo(exp)
  }
  else{
    a <- "row_number() > 0"
    my_exp <- rlang::quo(!! rlang::sym(a))
  }

  plot <-
    ggplot(
           mapping = aes(
             x = !!rlang::enquo(x),
             y = !!rlang::enquo(y)
           )) +
    geom_point(data = data) +
    geom_smooth(data = data, method = "lm")+
    geom_label_repel(data = data %>% filter(!!my_exp), 
                     mapping = aes(label = !!rlang::enquo(label.var)))
  return(plot)

}

这仍然使用 ifelse,但不需要像上面那样的所有额外代码。