dplyr和ggplot管道没有按预期工作 [英] dplyr and ggplot piping is not working as expected
问题描述
我找不到以下两个问题的解决方案:
首先我试试这个:
library(tidyverse)
gg< - mtcars%>%
mutate(group = ifelse(gear == 3,1,2))%>%
ggplot(aes(x = carb,y = drat))+ geom_point(shape = group)
图层中的错误(data = data,mapping = mapping,stat = stat,geom =
GeomPoint,:object'group'not found
这显然不起作用。 。$ group
也不是成功的。 / p>
第二个问题是,我无法在管道中调用保存的ggplot( gg
)。
gg < - mtcars%>>%
mutate(group = ifelse(gear == 3,1, 2))%>%
ggplot(aes(x = carb,y = drat))+ geom_point()
mtcars%>%
filter (vs == 0)%>%
gg + geom _point(aes(x = carb,y = drat),size = 4)
gg(。)中的错误:找不到函数gg
$ b
$ b感谢您的帮助!
$ b $ h1>编辑
经过很长时间,我发现了一个解决方案此处。必须在
{}
中设置完整的ggplot项。mtcars %>%
mutate(group = ifelse(gear == 3,1,2))%>%{
ggplot(。,aes(carb,drat))+
geom_point (shape =。$ group)}
解决方案你的
shape 定义在
。aes()
中,你可以得到想要的行为。要在aes()
之外使用shape
,您可以将它传递给一个值(即shape = 1
)。另请注意,当您将连续变量传递给<$时,group
会转换为离散变量,geom_point
会引发错误c $ c> shapelibrary(tidyverse)
gg < - mtcars%>%
mutate(group = ifelse(gear == 3,1,2))%>%
ggplot(aes(x = carb,y = drat))+
geom_point(aes(shape = as.factor(group)))
gg
其次,当
lhs%>%rhs 被调用时, %>%
,假定rhs
是一个函数。因此,如错误所示,您正在调用gg
作为函数。在数据框上调用一个函数作为一个函数(即gg(mtcars)
)不是一个有效的操作。
请参阅@docendo对如何使用
{}
来完成将图层添加到现有的ggplot
对象的问题进行了评论一个magrittr管道。I find no solution for these two following issues:
First I try this:
library(tidyverse) gg <- mtcars %>% mutate(group=ifelse(gear==3,1,2)) %>% ggplot(aes(x=carb, y=drat)) + geom_point(shape=group) Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomPoint,:object 'group' not found
which is obviously not working. But using something like this
.$group
is also not successfull. Of note, I have to specifiy the shape outside fromaes()
The second problem is this. I'm not able to call a saved ggplot (
gg
) within a pipe.gg <- mtcars %>% mutate(group=ifelse(gear==3,1,2)) %>% ggplot(aes(x=carb, y=drat)) + geom_point() mtcars %>% filter(vs == 0) %>% gg + geom_point(aes(x=carb, y=drat), size = 4) Error in gg(.) : could not find function "gg"
Thanks for your help!
Edit
After a long time I found a solution here. One has to set the complete ggplot term in
{}
.mtcars %>% mutate(group=ifelse(gear==3,1,2)) %>% { ggplot(.,aes(carb,drat)) + geom_point(shape=.$group)}
解决方案If you wrap your
shape
definition inaes()
you can get the desired behavior. To useshape
outside ofaes()
you can pass it a single value (ieshape=1
). Also note thatgroup
is converted to a discrete var,geom_point
throws an error when you pass a continuous var toshape
.library(tidyverse) gg <- mtcars %>% mutate(group=ifelse(gear==3,1,2)) %>% ggplot(aes(x=carb, y=drat)) + geom_point(aes(shape=as.factor(group))) gg
Second, the
%>%
operator, when called aslhs %>% rhs
, assumes that therhs
is a function. So as the error shows, you are callinggg
as a function. Calling a plot as a function on a dataframe (iegg(mtcars)
) isnt a valid operation.See @docendo discimus comment on the question for how to use
{}
to accomplish adding a layer to an existingggplot
object from a magrittr pipeline.这篇关于dplyr和ggplot管道没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!