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

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Tue Nov 16 20:08:33 CET 2021


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



More information about the R-help mailing list