[R] [External Email] Re: how to make the far right section of a smoother line look different from the rest of that line?

Deepayan Sarkar deep@y@n@@@rk@r @end|ng |rom gm@||@com
Wed Nov 17 06:44:09 CET 2021


On Wed, Nov 17, 2021 at 1:04 AM Christopher W Ryan via R-help
<r-help using r-project.org> wrote:
>
> Thanks Bert, that looks promising.
>
> panel.smoother() is from latticeExtra
>
> https://rdrr.io/cran/latticeExtra/man/panel.smoother.html

I'm a bit unsure about your premise. If I understand correctly, the
data for the last week is incomplete and may change. When it does, the
loess smooth is potentially affected for the preceding weeks as well
(in your case, with span = 0.3, upto around 30% of the preceding
weeks). So wouldn't it be misleading to suggest that the smoother
lines are "tentative" only for the last week or two?

It makes more sense to me to just highlight the last data point as
(potentially) incomplete.

Best,
-Deepayan

> --Chris Ryan
>
> On Tue, Nov 16, 2021 at 2:08 PM Bert Gunter <bgunter.4567 using gmail.com> wrote:
>
> > Where did you get panel.smoother()? There is no such panel function in
> > lattice.
> >
> > Is something like this what you want?
> >
> > x <- 1:100
> > y <- rnorm(100, mean =5)
> > end <- 91 # tentative smooth after this
> > xyplot(y ~x, cutoff = end, col1 = "black", col2 = "red"
> >        , panel = function(x, y,  col1, col2, cutoff) {
> >           sqleft <- seq_len(cutoff)
> >           sqright <- seq.int(cutoff +1, length(x))
> >           col <- rep(c(col1,col2), times = c(cutoff, length(x) - cutoff))
> >           panel.points(x, y, col = col)
> >           ylo <- predict(loess(y ~ x))
> >           panel.lines(x[sqleft], ylo[sqleft], col = col1, lwd =2)
> >           panel.lines(x[sqright], ylo[sqright], col = col2, lwd = 2, lty =
> > "dotted")
> >        })
> >
> > Notes:
> > 1. This works because of loess default to predict at given x's. Modify as
> > required if you change to another smoother or wish to use different points
> > at which to plot the smoother.
> > 2. This can almost certainly be done by creating a grouping variable to
> > separate the two plotting regimes and might be slicker and more robust with
> > that approach.
> >
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along and
> > sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> > On Tue, Nov 16, 2021 at 7:45 AM Christopher W Ryan via R-help <
> > r-help using r-project.org> wrote:
> >
> >> eclrs.3 %>%
> >> mutate(start.week = floor_date(realCollectionDate, unit = "week")) %>%
> >> group_by(start.week, k12) %>%
> >> summarise(n = n(), pctpos = 100 * mean(realResult))  %>%
> >> xyplot(pctpos ~ start.week | k12, col = "red", data = ., layout = c(1,2),
> >> ylab = "percent of test results positive", xlab = "specimen collection
> >> date",  strip = strip.custom(strip.names = c(TRUE, TRUE)),  sub = "The
> >> final week shown may not yet be complete so is likely inaccurate",   panel
> >> = function(...){
> >>     panel.xyplot(..., type = "p", cex = 0.8)
> >>     panel.smoother(..., span = 0.3, col.se = "red", alpha.se = 0.08, lwd
> >> =
> >> 2)})
> >>
> >> The above takes patient-level data, each record containing a date, and
> >> aggregates them by week according to that date, then plots 2 weekly time
> >> series, one for k12 and one for not-k12, each with a smoother. Note my
> >> disclaimer in the subtitle that "the final week shown may not yet be
> >> complete . . . ." since I might run this on any arbitrary day. How might I
> >> change the appearance of the smoother lines to emphasize to the viewer
> >> that
> >> the recent trends are tentative due to daily data still coming in?  For
> >> example, how might I make the far right-end stretch of the lines,
> >> representing the most recent week or two, dotted?
> >>
> >> Thanks.
> >>
> >> --Chris Ryan
> >>
> >>         [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> 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]]
>
> ______________________________________________
> 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.



More information about the R-help mailing list