Regression line exceeds confidence band

34 views
Skip to first unread message

Bob Muenchen

unread,
Dec 28, 2012, 12:09:28 PM12/28/12
to ggp...@googlegroups.com
Hi All,
 
I don't recall a regression line in ggplot2 exceeding the shaded confidence band, but that's what happened in the attached plot. Is this normal, and if so, under what conditions does it happen? The smallest x value is a bit of an outlier, so perhaps that's what causes it. Here's the code that did it:
 
# Create a function to apply by group
my_ggplot <- function(x) {
  print(
    ggplot(x, aes(pretest,posttest)) +
      geom_point() + geom_smooth(method = lm) +
      scale_x_continuous(limits = my_xlim) +
      scale_y_continuous(limits = my_ylim) +
      labs(title = unique(x$workshop) )
) }
 
and my_xlim was set to 58 86 to standardize the axes across groups. I'm using ggplot2 0.9.3 and plyr 1.8.
 
Thanks,
Bob
Reg_Line_Exceeds_Band.PNG

Ben Bolker

unread,
Dec 28, 2012, 12:14:19 PM12/28/12
to Bob Muenchen, ggp...@googlegroups.com
Because you set the limits with scale_y_continuous, the lowermost
segment of the confidence interval (which would transgress the minimum
y-bound) is getting clipped. This is standard (if sometimes annoying)
behaviour ... you can either use coord_cartesian() to set the limits, or
library(scales); scale_y_continuous(limits = my_ylim, oob = squish) ...

Ben Bolker
> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Ben Bond-Lamberty

unread,
Dec 28, 2012, 12:37:27 PM12/28/12
to Bob Muenchen, ggp...@googlegroups.com
Try using a single "+coord_cartesian( xlim=my_xlim, ylim=my_ylim )"
instead of those two scale_continuous calls.

Ben

Bob Muenchen

unread,
Dec 28, 2012, 2:17:52 PM12/28/12
to ggp...@googlegroups.com, Bob Muenchen
Hi Ben,
 
Those were both useful suggestions. The coord_cartesian worked, but it was so precise that it displayed only 1/4 of the extreme point! At first I didn't even realize that the point was there at all. It is almost invisible in the lower left corner. I used a paint package to add an arrow & some text so people would see it. 
 
The squish setting from the scales package did much better, showing the whole point and a bit of axis beyond it. Very nice!  Both plots are attached.
 
From help(squish) I never would have guessed it would have this effect. The examples are rather mysterious. Is the oob argument documented anywhere?
 
 
Thanks!
Bob 
squish.png
coord_cartisian.png
Reply all
Reply to author
Forward
0 new messages