【发布时间】:2018-08-31 14:03:50
【问题描述】:
这是 (using `rlang` for conditional labelling in `ggplot` using `ggrepel`) 的后续问题,它解决了我在自定义函数中遇到的问题,该函数使用表达式在标记数据点时过滤掉数据。但是答案又提出了一个我不知道如何解决的问题。
这是一个自定义函数,它使用rlang 评估用户输入的表达式以过滤掉数据,同时将标签附加到数据点。当不在列表列中使用时,此功能可以正常工作。例如-
# loading needed libraries
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)) {
my_exp <- rlang::enquo(exp)
}
else {
a <- "dplyr::row_number(x = .) > 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)
}
# using the function
label_adder(
data = datasets::iris,
x = Sepal.Length,
y = Sepal.Width,
label.var = Species,
exp = Sepal.Length > 7
)
但是当我使用与purrr::map 相同的功能时,它会失败。
# creating a list column
df.listcol <- datasets::iris %>%
dplyr::mutate(.data = ., Species2 = Species) %>% # just creates a copy of this variable
dplyr::group_by(.data = ., Species) %>%
tidyr::nest(data = .)
# running function on dataframe with list columns
df.listcol %>% # creates a nested dataframe with list column called `data`
dplyr::mutate( # creating a new list column of ggstatsplot outputs
.data = .,
plot = data %>%
purrr::map(
.x = .,
.f = ~label_adder(
data = .,
x = Sepal.Length,
y = Sepal.Width
)
)
)
#> Error in mutate_impl(.data, dots): Evaluation error: Evaluation error: object 'dplyr::row_number(x = .) > 0' not found..
但如果我通过指定label.var 和exp 来使用该函数,它就可以正常工作。
# running function on dataframe with list columns
df.listcol %>% # creates a nested dataframe with list column called `data`
dplyr::mutate( # creating a new list column of ggstatsplot outputs
.data = .,
plot = data %>%
purrr::map(
.x = .,
.f = ~label_adder(
data = .,
x = Sepal.Length,
y = Sepal.Width,
label.var = Species,
exp = Sepal.Length > 7
)
)
)
#> # A tibble: 3 x 3
#> Species data plot
#> <fct> <list> <list>
#> 1 setosa <tibble [50 x 5]> <S3: gg>
#> 2 versicolor <tibble [50 x 5]> <S3: gg>
#> 3 virginica <tibble [50 x 5]> <S3: gg>
所以我的问题是为什么在未指定label.var 和exp 时函数会失败以及如何解决此问题?
由reprex package (v0.2.0.9000) 于 2018 年 8 月 31 日创建。
【问题讨论】:
-
您能否真正访问上一个示例中的情节。我得到
Error: Columnlabel` 必须是一维原子向量或列表`。代码确实运行没有错误,但情节列表不可行 -
是的,那个错误是因为你在标签中使用了 Species 而不是 Species2。