问题相当简单 - 如果我采用 df 并对其执行一些过滤步骤,我希望能够将这些输出通过管道传输到 ggplot 函数中,并使用特定列的唯一值作为标题。 与 magrittr 管道和大括号一起使用:
starwars %>%
filter(hair_color == "blond") %>%
{
ggplot(data = ., aes(x = height, y = mass, col = birth_year)) +
geom_point() +
labs(title = unique(.$species))
}
不适用于等效基管:
starwars |>
filter(hair_color == "blond") |>
{
ggplot(data = _, aes(x = height, y = mass, col = birth_year)) +
geom_point() +
labs(title = unique(.$species))
}
Error in { :
function '{' not supported in RHS call of a pipe (<input>:3:7)
除了在调用ggplot之前分配df之外,还有其他选择吗?
我认为这有效:
library(dplyr)
starwars |>
filter(hair_color == "blond") |>
{ \(x)
ggplot(data = x, aes(x = height, y = mass, col = birth_year)) +
geom_point() +
labs(title = unique(x$species)) }()