是否有一种基本管道方式使用列的唯一值从ggplot管道到实验室

问题描述 投票:0回答:1

问题相当简单 - 如果我采用 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之外,还有其他选择吗?

r ggplot2 pipe
1个回答
0
投票

我认为这有效:

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)) }() 
© www.soinside.com 2019 - 2024. All rights reserved.