The general practice is to put + at the end of the line when creating ggplots, and %>% at the end of the line when using the pipe.
ggplot(data = mpg, aes(x = displ, y = hwy)) +
geom_point() +
stat_smooth()
delays <- flights %>%
group_by(dest) %>%
summarise(distance = mean(distance))
However, this makes commenting out particular lines harder. If I comment out the last line in either example, I'll get an error because the + or %>% is just "hanging out there".
I'd rather do it the way below. This lets me comment out lines as I wish, and to me seems aesthetically more pleasing (like a bulleted list).
ggplot(data = mpg, aes(x = displ, y = hwy))
+ geom_point()
+ stat_smooth()
delays <- flights
%>% group_by(dest)
%>% summarise(distance = mean(distance))
Yet these examples throw errors.
Why won't R let me do it either way?
And, if it must be one way, what are the reasons for the trailing + instead of the leading +?