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

Comments

Projects
None yet
2 participants
@izahn
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

This comment has been minimized.

Member

hadley commented Feb 2, 2017

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

izahn added a commit to izahn/ggplot2 that referenced this issue Feb 2, 2017

@izahn

This comment has been minimized.

Contributor

izahn commented Feb 2, 2017

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

izahn added a commit to izahn/ggplot2 that referenced this issue Feb 4, 2017

izahn added a commit to izahn/ggplot2 that referenced this issue Feb 28, 2017

izahn added a commit to izahn/ggplot2 that referenced this issue Mar 1, 2017

@hadley hadley closed this in #2029 Mar 1, 2017

hadley added 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.