带magrittr tee操作符的多个ggplots [英] Multiple ggplots with magrittr tee operator
问题描述
这可以正常工作了,但是我不知道为什么这个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运算符时,如下所示,它会抛出错误:ggplot2不知道如何处理类protoenvironment的数据。
mtcars%T>%
ggplot()+ geom_point(aes(cyl,mpg))%>%
ggplot()+ geom_point(aes(mpg,cyl))
任何人都可以解释为什么这段最后一段代码不起作用吗?
<$ p (ggplot(。)+ geom_point(aes(cyl,mpg)))}%>%
{ggplot(。) + geom_point(aes(mpg,cyl))}
或放弃% T>%
运算符,并使用具有%> T%操作的普通管道作为在这个答案中建议
techo < - function(x){
print(x )
x
}
mtcars%>%
{techo(ggplot(。)+ geom_point(aes(cyl,mpg)))}%>%
{ggplot(。)+ geom_point(aes(mpg,cyl))}
TFlick指出,%T>%操作符d的原因这里没有工作是因为操作的优先级:%any%
在 +
之前完成。
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(dplyr)
library(magrittr)
mtcars %T>%
qplot(x = cyl, y = mpg, data = ., geom = "point") %>%
qplot(x = mpg, y = cyl, data = ., geom = "point")
And this also works fine
mtcars %>%
{ggplot() + geom_point(aes(cyl, mpg)) ; . } %>%
ggplot() + geom_point(aes(mpg, cyl))
But when I use the tee operator, as below, it throws "Error: ggplot2 doesn't know how to deal with data of class protoenvironment".
mtcars %T>%
ggplot() + geom_point(aes(cyl, mpg)) %>%
ggplot() + geom_point(aes(mpg, cyl))
Can anyone explain why this final piece of code does not work?
Either
mtcars %T>%
{print(ggplot(.) + geom_point(aes(cyl, mpg)))} %>%
{ggplot(.) + geom_point(aes(mpg, cyl))}
or abandon the %T>%
operator and use an ordinary pipe with the "%>T%" operation made explicit as a new function as suggested in this answer
techo <- function(x){
print(x)
x
}
mtcars %>%
{techo( ggplot(.) + geom_point(aes(cyl, mpg)) )} %>%
{ggplot(.) + geom_point(aes(mpg, cyl))}
As TFlick noted, the reason the %T>% operator doesn't work here is because of the precedence of operations: %any%
is done before +
.
这篇关于带magrittr tee操作符的多个ggplots的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!