Couldn't find an answer to this -- in ggplot2, is it possible to facet on a function of a column, rather than on the value of the column directly?
Simple reproducible example:
Sample data:
df=data.frame(dat=c(1,2,5,5,7))
This works:
ggplot(df, aes(x=1:5, y=dat, colour=factor(dat > 3))) +
geom_point() + facet_grid(dat ~ .)
This does not:
ggplot(df, aes(x=1:5, y=dat, colour=factor(dat > 3))) +
geom_point() + facet_grid((dat > 3) ~ .)
One solution is to add a column just for the facet. This works:
df$facet=df$dat>3
ggplot(df, aes(x=1:5, y=dat, colour=factor(dat > 3))) +
geom_point() + facet_grid(facet ~ .)
But is there a way to do it without having to add a new column to the data.frame?