Skip to content
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

geom_smooth behaves like geom_path #2028

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

Comments

@izahn
Copy link
Contributor

@izahn 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 hadley commented Feb 2, 2017

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

@izahn
Copy link
Contributor Author

@izahn izahn commented Feb 2, 2017

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

@hadley hadley closed this in #2029 Mar 1, 2017
hadley added a commit that referenced this issue Mar 1, 2017
Fixes #2028
@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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants