How can I make ggplot plot geom_smooth(method="lm"), but only if it fits some criteria? For instance, if I only want to draw lines if the slope is statistically significant (i.e. p from the lm
fit is less than 0.01).
EDIT: Updated to a more complex example involving facets. Instead of generating the data from scratch, I modified the diamonds
data set.
library(ggplot2)
library(data.table)
data(diamonds)
set.seed(777)
d <- data.table(diamonds)
d[color %in% c("D","E"), c("x","y") := list(x + runif(1000, -5, 5),
y + runif(1000, -5, 5))]
plt <- ggplot(d) + aes(x=x, y=y, color=color) +
geom_point() + facet_grid(clarity ~ cut, scales="free")
plt + geom_smooth(method="lm")
What I would like is a way to plot all lines except those which do not have statistically significant slopes (i.e. D and E).
You can calculate the p-values by group and then subset in
geom_smooth
(per the commenters):UPDATE: Per your comment, try this, for example:
Note that since
dat
has noam
column,ggplot
just plots the same values indat
for each value ofam
. Of course you can add values foram
and control what's plotted facet by facet.UPDATE 2: I think this will take care of the faceting case. Note, however, that most of the regressions have p-values less than 0.05, probably because when you have lots of data, even tiny coefficients will be statistically significant.