Dplyr + ggplot2: Plotting does not work on pipelines

I want to build a subset of my frame. I am working with dplyr and ggplot2. My code only works with version 1, not version 2 via pipeline. Who cares?

Version 1 (plotting works):

data <- dataset %>% filter(type=="type1")
ggplot(data, aes(x=year, y=variable)) + geom_line()

      

Version 2 with piping (plotting doesn't work):

data %>% filter(type=="type1") %>% ggplot(data, aes(x=year, y=variable)) + geom_line()

      

Mistake:

Error in ggplot.data.frame(., data, aes(x = year,  : 
Mapping should be created with aes or aes_string

      

Thank you for your help!

+2


source to share


3 answers


Solution for version 2: point. instead of data:



data %>% filter(type=="type1") %>% ggplot(., aes(x=year, y=variable)) + geom_line()

      

+10


source


I usually do this, which also removes the need for .

:



library(dplyr)
library(ggplot2)

mtcars %>% 
  filter(cyl == 4) %>%
  ggplot +
  aes(
    x = disp,
    y = mpg
  ) + 
  geom_point()

      

+4


source


When typing with a pipe, while re-entering the data name as shown in the figure below, the function confuses the sequence of arguments.

data %>% filter(type=="type1") %>% ggplot(***data***, aes(x=year, y=variable)) + geom_line()

Hope it works for you.

-1


source







All Articles