Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot geom_smooth exclude negative values

Tags:

r

ggplot2

I am trying to plot a linear regression with standard error (se) using ggplot2 geom_smooth which excludes negative values. Using scale_y_continuous unfortunately truncates part of the standard error fill. How can I get the se fill area to smoothly end at y=0?

See example (which uses y=10 rather than y=0, but same process):

ymax<-max(mtcars$mpg)
myplot<- ggplot(data=mtcars, aes(x=wt, y=mpg)) +  
geom_smooth(method=lm,   se=TRUE,fill = "#3399FF", colour="#0000FF",size =1)  +
geom_point(shape=20, size=2) +
scale_y_continuous(limits = c(10, ymax)) 
suppressMessages(print(myplot))  

I wish I could post the graph, but I have just joined stack overflow and don't have enough reputation points to post images. The graph shows the regression line ending at y=10 as expected, however the se fill ends as a vertical edge (not horizontal with the line below which se should be excluded). Thanks, and sorry I can't post the image :)

like image 335
treetopdewdrop Avatar asked Oct 31 '25 04:10

treetopdewdrop


1 Answers

With the new version of ggplot we now need:

 scale_y_continuous(limit=c(10,NA),oob=squish)
like image 117
thestral Avatar answered Nov 02 '25 20:11

thestral



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!