我试图弄清楚为什么tee运算符%T>%在我将数据传递给ggplot命令时不能工作。
这个很好用
library(ggplot2)
library(dplyr)
library(magrittr)
mtcars %T>%
qplot(x = cyl, y = mpg, data = ., geom = "point") %>%
qplot(x = mpg, y = cyl, data = ., geom = "point")这也很好
mtcars %>%
{ggplot() + geom_point(aes(cyl, mpg)) ; . } %>%
ggplot() + geom_point(aes(mpg, cyl))但是当我使用tee操作符时,如下面所示,它会抛出"Error: ggplot2不知道如何处理类原型环境的数据“。
mtcars %T>%
ggplot() + geom_point(aes(cyl, mpg)) %>%
ggplot() + geom_point(aes(mpg, cyl))有人能解释为什么这最后一段代码不起作用吗?
发布于 2014-12-08 00:36:00
任一
mtcars %T>%
{print(ggplot(.) + geom_point(aes(cyl, mpg)))} %>%
{ggplot(.) + geom_point(aes(mpg, cyl))}或者放弃%T>%操作符,使用普通管道,将"%>T%“操作显式地作为suggested in this answer的新函数。
techo <- function(x){
print(x)
x
}
mtcars %>%
{techo( ggplot(.) + geom_point(aes(cyl, mpg)) )} %>%
{ggplot(.) + geom_point(aes(mpg, cyl))}正如TFlick所指出的,%T>%操作符不能在这里工作的原因是操作的优先级:%any%是在+之前完成的。
发布于 2014-12-03 05:45:49
我认为你的问题与行动顺序有关。+比%T>%操作符强(根据?Syntax帮助页面)。在添加data=之前,您需要将ggplot参数传递给geom_point,否则事情就会变得一团糟。我想你想
mtcars %T>%
{print(ggplot(.) + geom_point(aes(cyl, mpg)))} %>%
{ggplot(.) + geom_point(aes(mpg, cyl))}它使用函数“短手”表示法。
发布于 2018-01-06 01:06:39
请注意,返回的ggplot对象是一个带有$data字段的列表。这是可以利用的。就我个人而言,我认为这种风格更干净:)
ggpass=function(pp){
print(pp)
return(pp$data)
}
mtcars %>%
{ggplot() + geom_point(aes(cyl, mpg))} %>% ggpass() %>%
{ggplot() + geom_point(aes(mpg, cyl))}https://stackoverflow.com/questions/27264266
复制相似问题