Geom_abline (...) displays data multiple times?

Do you use when you use geom_abline(...)

(and geom_vline

and geom_hline

) multiple overlapping one line using the "naive"?

For example, let's say we are interested in the following faceted scatter plot:

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-string to it is as follows:

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

      

Is this the correct way? Specifically, is the above graph ab-line 30 times not specified in every aspect? For example, it looks like this:

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 creates something visually incomprehensible from the first plot. My question is about the correct way to use these ggplot functions.

+3


source to share


1 answer


geom_abline

takes care of several lines in the same place, making the values ​​unique. You can also make sure that on startup, for example p + geom_abline(aes(slope = m, intercept = b), color = 'red', alpha = .1)

- if it were 30 lines in the same place, they would be opaque.



+1


source







All Articles