banbh - 3 years ago 58
R Question

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

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.

`geom_abline` takes care already of multiple lines at the same spot by making the values unique. You can also verify that by running e.g. `p + geom_abline(aes(slope = m, intercept = b), color = 'red', alpha = .1)` - if it were 30 lines at the same spot, they would be opaque.