Does geom_abline(...) plot data multiple times?

Viewed 515

Does using geom_abline(...) (as well as geom_vline and geom_hline) result in multiple overplotting of the same line when used "naively"?

For example, say we are interested in the following faceted scatterplot:

library(ggplot2)
library(dplyr)

k <- 4
data.frame(id = letters[1:k], 
           m = rnorm(k), 
           b = rnorm(k))[rep(1:k, 30),] %>%
  mutate(x = rnorm(n()),
         eps = 0.1*rnorm(n()),
         y = m*x + b + eps) %>%
  ggplot(aes(x, y)) + 
  geom_point() +
  facet_wrap(~ id) ->
  p

The easiest way to add an ab-line to it is as follows:

print(p + geom_abline(aes(slope = m, intercept = b), color = 'red'))

Is that the "right way" to do it? In particular, doesn't the above plot the ab-line 30 times in each facet? For example this makes it seem like it does:

print(p + geom_abline(aes(slope = m, intercept = b + eps), color = 'red'))

If so, is it better to do something like this?

print(p + geom_abline(aes(slope = m, intercept = b), 
                      data = Z %>% group_by(id) %>% summarize(m = unique(m), b = unique(b)),
                      color = 'red'))

Note that this produces something visually indistiguishable from the first plot. My question is about the right way to use these ggplot functions.

1 Answers
Related