I'm trying to figure out why my lm()
estimates are different than the geom_smooth
for the same data and formula. Specifically the slope for my grouping variable "cat" level 5 is >0 in the lm()
output, but <0 in the geom_smooth (and hence the plot does not appear to reflect the summary table).
Here are the data. (Easier than coming up with example data that behave similarly.)
The model: summary(lm(data=df, y~x*cat))
Notice the slope for x:cat5
is >0.
The plot:
library(ggplot2)
plt <- ggplot(df, aes(x=x, y=y, group=cat)) +
geom_smooth(method="lm", show.legend=FALSE) +
facet_wrap(~cat, nrow=1) +
geom_point(aes(color=color)
Get the geom_smooth estimates (following @Pedro Aphalo's answer here):
library(ggpmisc)
my.formula <- y~x
plt + stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE)
Notice the slope in facet 5 is <0. Are lm()
and geom_smooth
using a different sum of squares or something? Which version do I report in a paper? If possible, I'd like to get the two to agree so I can use the plot with geom_smooth
and the summary table from lm()
in the paper. Thanks!