thomasp85 / ggforce

Accelerating ggplot2
https://ggforce.data-imaginist.com
Other
916 stars 106 forks source link

How do I add `geom_abline` to each `facet_matrix` facet? #228

Closed pointOfive closed 8 months ago

pointOfive commented 3 years ago
mpg %>% add_column(slope=1) %>% add_column(intercept=0) %>%
ggplot(aes(x = .panel_x, y = .panel_y, fill = drv, colour = drv)) + 
  geom_point(shape = 16, size = 0.5, position = 'auto') + 
  geom_autodensity(alpha = 0.3, colour = NA, position = 'identity') + 
  geom_abline(aes(intercept=intercept, slope=slope)) +
  geom_smooth(aes(colour = NULL, fill = NULL)) + 
  facet_matrix(vars(fl, displ, hwy), layer.diag = 2, layer.continuous = TRUE,
               layer.mixed = TRUE, layer.discrete = TRUE) -> out

only puts it on the first pairwise scatter facets.

Alternatively, adding a single line to the "flurry-of-facets" code here

ggplot(mpg, aes(x = .panel_x, y = .panel_y, fill = drv, colour = drv)) + 
  geom_point(shape = 16, size = 0.5, position = 'auto') + 
  geom_autodensity(alpha = 0.3, colour = NA, position = 'identity') + 
  geom_smooth(aes(colour = NULL, fill = NULL)) + 
  facet_matrix(vars(fl, displ, hwy), layer.diag = 2, layer.continuous = TRUE,
               layer.mixed = -3, layer.discrete = -3)

just errors like this:

Error: Can't subset columns that don't exist.
x Column `fl` doesn't exist.
thomasp85 commented 8 months ago

I'm afraid that I can't reproduce the last error you describe. As for why you only see the line in the first pairwise panels it's because the line is not visible in the others as they are in other areas of the coordinate system