geom_smooth()

725 views
Skip to first unread message

Henri-Paul Indiogine

unread,
Oct 17, 2012, 1:31:25 PM10/17/12
to ggp...@googlegroups.com
Greetings!

I am getting more comfortable in using ggplot2 and prefer it above any
other plotting library in R-project. Thanks for this wonderful tool.

I am using geom_smooth() for my scatter charts using geom_point().
I would like a trend line added to the scattered points and use:

geom_smooth(aes(color = factor(variable)), se = FALSE)

I have many points with value = zero and thus I have noticed that the
line goes sometimes below the x-axis. All my data are positive
integers and thus a trend line in negative territory does not make
sense.

Could this be fixed? I looked into the stat_smooth() documentation,
but could find anything about it. By default it uses the "loess"
method. Do I need to change that or do I need to pass any parameters?

Thanks in advance and best regards,

Henri


--
Henri-Paul Indiogine

Curriculum & Instruction
Texas A&M University
TutorFind Learning Centre
http://www.tutorfind.ca

Email: hindi...@gmail.com
Skype: hindiogine

Ben Bond-Lamberty

unread,
Oct 17, 2012, 8:25:55 PM10/17/12
to ggp...@googlegroups.com
Henri-Paul,

Providing a reproducible example will generally get you better answers, but...

If the smoothing function isn't behaving how you want, you can try a
different one--for example, geom_smooth(method="lm")--or provide your
own 'better-behaved' function, i.e. one that provides a better model
of whatever process is producing your data. The geom_smooth help page
gives an example of this, I believe.

Ben
> --
> 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

ONKELINX, Thierry

unread,
Oct 18, 2012, 4:32:57 AM10/18/12
to Henri-Paul Indiogine, ggp...@googlegroups.com
Are the values counts (= positive integers)? In that case a poisson regression is more suitable as smoother. E.g. geom_smooth(method = 'glm', family = 'poisson')

set.seed(12345)
library(ggplot2)
library(mgcv)
Dataset <- data.frame(A = seq(-10, 10, length = 1001))
Dataset$B <- rpois(nrow(Dataset), exp(2 + 0.1 * Dataset$A - 0.1 * Dataset$A ^ 2))
ggplot(Dataset, aes(x = A, y = B)) + geom_point() + geom_smooth()
ggplot(Dataset, aes(x = A, y = B)) + geom_point() + geom_smooth(method = "glm", family = "poisson")
ggplot(Dataset, aes(x = A, y = B)) + geom_point() + geom_smooth(method = "glm", family = "poisson", formula = y ~ poly(x, 2))
ggplot(Dataset, aes(x = A, y = B)) + geom_point() + geom_smooth(method = "gam", family = "poisson")
ggplot(Dataset, aes(x = A, y = B)) + geom_point() + geom_smooth(method = "gam", family = "poisson", formula = y ~ s(x))

ggplot(Dataset, aes(x = A, y = B)) + geom_smooth(method = "gam", family = "poisson", formula = y ~ s(x)) + coord_cartesian(ylim = (c(-0.1, 1))) #note that even the confidence intervals are non-negative

Best regards,

Thierry

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry....@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey


-----Oorspronkelijk bericht-----
Van: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] Namens Henri-Paul Indiogine
Verzonden: woensdag 17 oktober 2012 19:31
Aan: ggp...@googlegroups.com
Onderwerp: geom_smooth()
--
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
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.

Henri-Paul Indiogine

unread,
Oct 18, 2012, 11:37:32 AM10/18/12
to ONKELINX, Thierry, ggp...@googlegroups.com
Thierry, bedankt voor the suggesties.

Beste,
Henk (a.k.a. Henri)

2012/10/18 ONKELINX, Thierry <Thierry....@inbo.be>:
Reply all
Reply to author
Forward
0 new messages