Multiple ggplots with magrittr tee operator

前端 未结 3 975
栀梦
栀梦 2021-02-14 23:04

I am trying to figure out why the tee operator, %T>%, does not work when I pass the data to a ggplot command.

This works fine

library(ggplot2)
library(dp         


        
3条回答
  •  独厮守ぢ
    2021-02-15 00:10

    I think your problem has to do with order of operations. The + is stronger than the %T>% operator (according to the ?Syntax help page). You need to pass in the data= parameter to ggplot before you add the geom_point otherwise things get messy. I think you want

    mtcars %T>%
      {print(ggplot(.) + geom_point(aes(cyl, mpg)))} %>%
      {ggplot(.) + geom_point(aes(mpg, cyl))}
    

    which uses the functional "short-hand" notation

提交回复
热议问题