Smoothing goes below 0

299 views
Skip to first unread message

Frank Walraven

unread,
Jun 20, 2017, 11:45:14 AM6/20/17
to ggplot2
I just started working with ggplot today and I'm trying to make a smooth plot of a dataset with 1001 lines. The problem is that when I smooth it the values go below 0, even though all values in the dataset are above 0. I already tried using different methods, but those will just make the line straight for some reason. Right now it says it's using the gam method when I run it and that seems to work (aside from the below 0 values that is). I am looking for some way to force the smooth to keep the values above (or equal to) 0.

non-smoothed graph: http://i.imgur.com/mq38LUK.png

current R code:
#(smoothed) lineplot of median pucks collected over time with and without penalty:
median_pucks_over_time <- ggplot(stats_yes_pen_pucks_collected, aes(x=V1, y=V3, col="with penalty")) +
  geom_ribbon(aes(ymin=stats_yes_pen_pucks_collected$V12, ymax=stats_yes_pen_pucks_collected$V13), fill="darkgreen", linetype=2, alpha=0.3, colour=NA) +
  geom_smooth(se = FALSE) +
  geom_ribbon(aes(ymin=stats_no_pen_pucks_collected$V12, ymax=stats_no_pen_pucks_collected$V13), fill="blue", linetype=2, alpha=0.3, colour=NA) +
  geom_smooth(data = stats_no_pen_pucks_collected, se = FALSE, aes(col="without penalty")) +
  scale_color_manual("Dataset", values = c("with penalty" = "darkgreen", "without penalty" = "blue")) + 
  labs(x = "time", y = "median number of pucks collected") + 
  theme(panel.background = element_rect(fill = 'white', colour = 'grey'), panel.grid.minor = element_line(colour = "grey"))

Could anyone point me in the right direction? :)


P.S. This might be a repost, I already tried to post here but it did seems to actually have been posted? 

Thierry Onkelinx

unread,
Jun 20, 2017, 5:27:42 PM6/20/17
to Frank Walraven, ggplot2
Your data is not gaussian distributed. So you need to specify the distribution.

ggplot(stats_yes_pen_pucks_collected, aes(x = V1, y = V3) +
   geom_smooth(method = "gam", formula = y ~ s(x, bs = "cs", k = 4), method.args = list(family = poisson))

Best regards,


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

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

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

Reply all
Reply to author
Forward
0 new messages