-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Labels
Description
I noticed that geom_smooth
behaves like geom_path
rather than geom_line
. For example,
library(ggplot2)
ggplot(transform(mtcars,
pred.mpg = predict(lm(mpg ~ poly(hp, 2),
data = mtcars),
interval = "confidence"))) +
geom_smooth(aes(x = hp,
y = pred.mpg.fit,
ymin = pred.mpg.lwr,
ymax = pred.mpg.upr),
stat = "identity")
Whereas ordering by the x-axis variable produces the desired result:
ggplot(transform(mtcars,
pred.mpg = predict(lm(mpg ~ poly(hp, 2),
data = mtcars),
interval = "confidence"))[order(mtcars$hp), ]) +
geom_smooth(aes(x = hp,
y = pred.mpg.fit,
ymin = pred.mpg.lwr,
ymax = pred.mpg.upr),
stat = "identity")
I looked at the geom_smooth
code, but I don't understand why it behaves this way. It looks like GeomSmooth
calls GeomLine
, and GeomLine
does
setup_data = function(data, params) {
data[order(data$PANEL, data$group, data$x), ]
}
so I would have thought ordering the data myself would make no difference.