Skip to content

geom_smooth behaves like geom_path #2028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
izahn opened this issue Feb 2, 2017 · 2 comments
Closed

geom_smooth behaves like geom_path #2028

izahn opened this issue Feb 2, 2017 · 2 comments
Labels
feature a feature request or enhancement layers 📈

Comments

@izahn
Copy link
Contributor

izahn commented Feb 2, 2017

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")

produces
smooth_example

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")

smooth_example2

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.

@hadley
Copy link
Member

hadley commented Feb 2, 2017

It probably just calls the draw method, not the setup_data() method.

@izahn
Copy link
Contributor Author

izahn commented Feb 2, 2017

Thanks, I've submitted #2029 to address this.

@hadley hadley added feature a feature request or enhancement layers 📈 labels Feb 13, 2017
hadley pushed a commit that referenced this issue Mar 1, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement layers 📈
Projects
None yet
Development

No branches or pull requests

2 participants