5

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)}
Community
  • 1
  • 1
Roman
  • 17,008
  • 3
  • 36
  • 49
  • 4
    try `geom_point(aes(shape=factor(group)))` and for the second part you need `{}` as in `mtcars %>% filter(vs == 0) %>% {gg + geom_point(data = ., aes(x=carb, y=drat), size = 4)}` – talat May 17 '17 at 13:00
  • In the first part I need this `ggplot(mtcars, aes(x=carb, y=drat)) + geom_point(shape=ifelse(mtcars$gear==3,1,2))`. Thum up for the second part! – Roman May 17 '17 at 13:08
  • i had never seen the `{}` trick with `ggplot` before. nice solution – Adam Spannbauer May 17 '17 at 13:14
  • @docendodiscimus Then piping is not possible with ggplot if one have to set a variable outside `aes()`? – Roman May 18 '17 at 07:42
  • And I'm wondering why the question has two votes for closing? – Roman May 18 '17 at 07:44
  • Why do you need to set shape outside of `aes`? – drmariod Apr 03 '18 at 14:15
  • @drmariod just for curiostiy and because it makes no sense in my eyes regarding the piping rules. But honestly I have no idea why I needed this at that time.... – Roman Apr 03 '18 at 14:18
  • I think, if you want to access a column, this is only possible within the `aes` function. If you try this outside, it tries to access a variable. You can set this separately and access it like this `group <- ifelse(mtcars$gear==3,1,2) ; ggplot(mtcars, aes(x=carb, y=drat)) + geom_point(shape=factor(group))`. But I guess this is not the best way. With the `{}` it should also be possible to use within piping. – drmariod Apr 03 '18 at 14:26

1 Answers1

2

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.

Adam Spannbauer
  • 2,707
  • 1
  • 17
  • 27
  • But I want this: `ggplot(mtcars, aes(x=carb, y=drat)) + geom_point(shape=ifelse(mtcars$gear==3,1,2))`. So I can't use piping in both cases? – Roman May 17 '17 at 13:06
  • You first code block accomplishes that call to `ggplot`, but you can't predefine the plot and then call it as a function. If you want to call the code used to generate `gg` on a different dataframe then you can wrap it in a function before using in the pipeline – Adam Spannbauer May 17 '17 at 13:12
  • Thanks. But nevertheless I need to set the shape outside `aes()`. Seems not possible with piping then. – Roman May 18 '17 at 07:39