hadley/ggplot2 — Issue #398: Unexpected behavior from geom_abline when adding lines to marginal facets
Author: thomasjwood
Created: 2012-02-09 06:21:28+00:00
State: open

I want to put a regression line on the marginal facets of a facet_grid, but I come up with a strange quirk where there are redundant lines on non-marginal facets also.

library(ggplot2)
library(plyr)
data(diamonds)

Use plyr to build a data frame with the slopes and intercepts

regdf <- ddply(diamonds, .(cut), function(i)
lm(price ~ carat, data = i)$coefficients[1:2])

resolve some naming issues

regdf$color <- "(all)"
names(regdf)[2] <- "intercept"

p1 <- ggplot() + geom_point(aes(carat, price), data = diamonds, alpha = .4) +
facet_grid(color ~ cut, margins = T) +
geom_abline(aes(intercept = intercept, slope = carat), color = "red", data = regdf)

![the strange behavior is obvious](http://i.stack.imgur.com/kDl4u.png)