[R] Skip jumps in curve

Bill Dunlap w||||@mwdun|@p @end|ng |rom gm@||@com
Tue Feb 13 22:54:23 CET 2024


Here is the skeleton of a function that lets you supply a function that
will be applied to diff(y) to say if this next point should be connected by
a line to the previous point.

p <- function (x, y = NULL, dy = diff(y), predicate = function(dy)
abs(dy)>2, ...,
  xlab = if (!missing(x)) deparse1(substitute(x)),
  ylab = if (!missing(y)) deparse1(substitute(y)), log = "", type="l")
{
  force(xlab)
  force(ylab)
  xy <- xy.coords(x, y, xlab, ylab, log)
  x <- xy$x
  y <- xy$y
  group <- cumsum( c(TRUE, !is.finite(dy) | predicate(dy)))
  plot(x, y, xlab=xy$xlab, ylab=xy$ylab, log=log, ..., type="n")
  sx <- split(x, group)
  sy <- split(y, group)
  lapply(seq_len(max(group)), function(i)points(type=type, sx[[i]],
sy[[i]]))
  invisible()
}


E.g., plot the tangent function where we know that the discontinuities are
where diff(y) is negative:

par(mfrow=c(2,1))
plot(tan(seq(0,10,by=1/4)), type="b")
p(tan(seq(0,10,by=1/4)), predicate = function(dy)dy<0, type="b")

-Bill

On Tue, Feb 13, 2024 at 10:54 AM Duncan Murdoch <murdoch.duncan using gmail.com>
wrote:

> It should be pretty easy to generalize my version of the `plot.gamma()`
> function to a version of `curve()` with an extra `discontinuities`
> argument.
>
> Duncan Murdoch
>
> On 13/02/2024 1:44 p.m., Leo Mada wrote:
> > Dear Duncan,
> >
> > Thank you very much for the response. I suspected that such an option
> > has not been implemented yet.
> >
> > The plot was very cluttered due to those vertical lines. Fortunately,
> > the gamma function is easy to handle. But the feature remains on my
> > wishlist as useful more in general.
> >
> > Sincerely,
> >
> > Leonard
> >
> > ------------------------------------------------------------------------
> > *From:* Duncan Murdoch <murdoch.duncan using gmail.com>
> > *Sent:* Tuesday, February 13, 2024 6:05 PM
> > *To:* Leo Mada <leo.mada using syonic.eu>; r-help using r-project.org
> > <r-help using r-project.org>
> > *Subject:* Re: [R] Skip jumps in curve
> > On 13/02/2024 10:29 a.m., Leo Mada via R-help wrote:
> >> Dear R-Users,
> >>
> >> Is there a way to skip over without plotting the jumps/discontinuities
> in curve()?
> >>
> >> I have not seen such an option, but maybe I am missing something.
> >>
> >> plot.gamma = function(xlim = c(-6, -1), ylim = c(-1,3), hline = NULL, n
> = 1000) {
> >>     curve(gamma(x), from = xlim[1], to = xlim[2], ylim=ylim, n=n);
> >>     if( ! is.null(hline)) abline(h = hline, col = "green");
> >> }
> >>
> >> Euler = 0.57721566490153286060651209008240243079;
> >> plot.gamma(hline = Euler)
> >>
> >> Adding an option to the function curve may be useful:
> >> options = c("warn", "silent", "unconnected")
> >>
> >> This is part of some experiments in math; but that's another topic. For
> latest version:
> >> https://github.com/discoleo/R/blob/master/Math/Integrals.Gamma.Inv.R
> > <https://github.com/discoleo/R/blob/master/Math/Integrals.Gamma.Inv.R>
> >
> > If you know where the discontinuities are, plot multiple times with the
> > discontinuities as endpoints:
> >
> > plot.gamma = function(xlim = c(-6, -1), ylim = c(-1,3), hline = NULL, n
> > = 1000) {
> >     start <- floor(xlim[1]):floor(xlim[2])
> >     end <- start + 1
> >
> >     start[1] <- xlim[1]
> >     end[length(end)] <- xlim[2]
> >
> >     n <- round(n/length(start))
> >
> >     curve(gamma(x), from = start[1], to = end[1], ylim=ylim, n=n, xlim =
> > xlim)
> >     for (i in seq_along(start)[-1])
> >       curve(gamma(x), from = start[i], to = end[i], add = TRUE, n)
> >     if( ! is.null(hline)) abline(h = hline, col = "green");
> > }
> >
> > Euler = 0.57721566490153286060651209008240243079;
> > plot.gamma(hline = Euler)
> >
> > If you don't know where the discontinuities are, it would be much
> > harder, because discontinuities can be hard to detect unless the jumps
> > are really big.
> >
> > Duncan Murdoch
> >
> >
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list