Prevent negative values using geom_smooth

2,329 views
Skip to first unread message

Thiago V. dos Santos

unread,
May 8, 2017, 5:49:17 PM5/8/17
to ggplot2
Dear all,

I am trying to plot a drought stress factor in the soil as a function of soil moisture.

My data look like this:

> DF
    sm      stressm
1  0.0 1.0000000000
2  0.1 1.0000000000
3  0.2 1.0000000000
4  0.3 0.3585328089
5  0.4 0.0112217390
6  0.5 0.0007326102
7  0.6 0.0007326102
8  0.7 0.0007326102
9  0.8 0.0007326102
10 0.9 0.0007326102
11 1.0 0.0007326102

When plotting this data in ggplot2 using geom_smooth to fit a curve, this is what I get:

library(ggplot2)


DF
= structure(list(sm = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8,
0.9, 1), stressm = c(1, 1, 1, 0.358532808851159, 0.0112217390323767,
0.00073261019713633, 0.00073261019713633, 0.00073261019713633,
0.00073261019713633, 0.00073261019713633, 0.00073261019713633
)), .Names = c("sm", "stressm"), row.names = c(NA, -11L), class = "data.frame")


ggplot
(DF, aes(x=sm,y=stressm)) +
     geom_point
() +
     geom_smooth
(method='loess', se=F, colour='black') +
     theme_bw
(base_size=16)



What would you recommend to improve that fitted curve based on my dataset? Specifically, I am trying to:

1) get a flatter line when soil moisture is between 0 and 0.2
2) prevent the negatives values of stress when soil moisture is around 0.5

Many thanks for any thoughts.

Ben Bolker

unread,
May 8, 2017, 8:49:51 PM5/8/17
to Thiago V. dos Santos, ggplot2
This works, although it gives warnings (because of complete separation, and because of non-integer values in the GLM)

gg0 <- ggplot(DF, aes(x=sm,y=stressm)) +
  geom_point() +
  theme_bw(base_size=16)
gg0 + geom_smooth(method="glm",se=FALSE,
                  method.args=list(family=binomial))

--
--
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+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thiago V. dos Santos

unread,
May 8, 2017, 8:56:42 PM5/8/17
to ggplot2
Hi Ben,

Your answer solved my problem: the warnings generated don't prevent code execution. 

Thanks a lot!
To unsubscribe: email ggplot2+u...@googlegroups.com

More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.

Thiago V. dos Santos

unread,
May 8, 2017, 9:04:07 PM5/8/17
to ggplot2
Hi Ben, 

Just a quick follow-up: I found out that replacing "family=binomial" with "family=quasibinomial" gets rid of the warning messages!

Thanks for your help again,
Thiago.


On Monday, May 8, 2017 at 7:49:51 PM UTC-5, Ben Bolker wrote:
To unsubscribe: email ggplot2+u...@googlegroups.com

More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages