dplyr和ggplot管道没有按预期工作 [英] dplyr and ggplot piping is not working as expected

查看:178
本文介绍了dplyr和ggplot管道没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到以下两个问题的解决方案:

首先我试试这个:

  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> shape

  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

其次,当 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 from aes()

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 in aes() you can get the desired behavior. To use shape outside of aes() you can pass it a single value (ie shape=1). Also note that group is converted to a discrete var, geom_point throws an error when you pass a continuous var to shape.

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 as lhs %>% rhs, assumes that the rhs is a function. So as the error shows, you are calling gg as a function. Calling a plot as a function on a dataframe (ie gg(mtcars)) isnt a valid operation.

See @docendo discimus comment on the question for how to use {} to accomplish adding a layer to an existing ggplot object from a magrittr pipeline.

这篇关于dplyr和ggplot管道没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆