Christopher W Ryan
2019-Nov-05 16:40 UTC
[R] how to place a rug on only the x-axis in a scatterplot with lattice
The following produces a scatterplot with rugs on both the vertical and horizontal axes. library(dplyr) library(stringr) library(lattice) library(latticeExtra) ## ..... xyplot(scheduleInterval ~ calledForApptDate, data = dd.2, xlab = "Date patient called for appointment", ylab = "Days in the future that patient was scheduled", panel = function(...) { panel.xyplot(..., col = "red") panel.smoother(..., span = 0.9, se = FALSE) panel.rug(...) }) I'd like a rug to appear only on the horizontal axis. None of the following seem to be the correct syntax: panel.rug(..., y = NULL) panel.rug(..., y = FALSE) panel.rug(x) panel.rug(x = ...) This does the job: xyplot(scheduleInterval ~ calledForApptDate, data = dd.2, xlab = "Date patient called for appointment", ylab = "Days in the future that patient was scheduled", panel = function(...) { panel.xyplot(..., col = "red") panel.smoother(..., span = 0.9, se = FALSE) panel.rug(x = dd.2$calledForApptDate) }) but seems inadvisable. Shouldn't I be making use of ... for passing arguments through to the panel.rug() function? Specifying a variable in a dataframe by name isn't generalizable. Thanks. --Chris Ryan [[alternative HTML version deleted]]
Bert Gunter
2019-Nov-05 17:28 UTC
[R] how to place a rug on only the x-axis in a scatterplot with lattice
Here's how you pass an argument down to the panel function. foo <- runif(30,0,5) y <- rnorm(30, mean = 10) xyplot(y~foo, panel = function(x,...) { panel.xyplot(x,..., col = "red") panel.rug(x, col="black") }) Cheers, Bert 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 5, 2019 at 8:41 AM Christopher W Ryan <cryan at binghamton.edu> wrote:> The following produces a scatterplot with rugs on both the vertical and > horizontal axes. > > library(dplyr) > library(stringr) > library(lattice) > library(latticeExtra) > ## ..... > xyplot(scheduleInterval ~ calledForApptDate, data = dd.2, xlab = "Date > patient called for appointment", ylab = "Days in the future that patient > was scheduled", > panel = function(...) { > panel.xyplot(..., col = "red") > panel.smoother(..., span = 0.9, se = FALSE) > panel.rug(...) > }) > > I'd like a rug to appear only on the horizontal axis. None of the > following seem to be the correct syntax: > > panel.rug(..., y = NULL) > panel.rug(..., y = FALSE) > panel.rug(x) > panel.rug(x = ...) > > This does the job: > > xyplot(scheduleInterval ~ calledForApptDate, data = dd.2, xlab = "Date > patient called for appointment", ylab = "Days in the future that patient > was scheduled", > panel = function(...) { > panel.xyplot(..., col = "red") > panel.smoother(..., span = 0.9, se = FALSE) > panel.rug(x = dd.2$calledForApptDate) > }) > > but seems inadvisable. Shouldn't I be making use of ... for passing > arguments through to the panel.rug() function? Specifying a variable in a > dataframe by name isn't generalizable. > > Thanks. > > --Chris Ryan > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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]]
Bert Gunter
2019-Nov-05 22:41 UTC
[R] how to place a rug on only the x-axis in a scatterplot with lattice
For the record, I left out a key word in my prior "explanation", which I have corrected below. I also needed to clarify something, as my original wording is confusing. Sorry about that. Bert On Tue, Nov 5, 2019 at 11:09 AM Bert Gunter <bgunter.4567 at gmail.com> wrote:> Lattice functions pass down their **unrecognized** arguments to the panel > function. Once you know this, argument handling is controlled by R's usual > rules, especially with regard to the "..." argument. Hence, you may wish to > review tutorials on argument passing in function calls in R. > > But briefly, the following may be informative: > > xyplot(y ~ foo, > panel = function(...){## x and y in ... arguments > panel.xyplot(...) > panel.rug(..., col = "black") > }) > > will pass down all the **unrecognized** arguments in the xyplot call > (there are none here) **plus** the x and y arguments obtained from the > formula method. Thus panel.rug(...) will get *both* x = and y = arguments > and will accordingly put rugs on BOTH axes, as you saw. > > To prevent this, you only want to pass down the x argument, not y. Here > are several ways to do this (check them!): > > ## pass down x in ... but pass y explicitly and set it to NULL in > panel.rug call > xyplot(y ~ foo, > panel = function(y,...) { ## x is in ... arguments > panel.xyplot(y,..., col = "red") > panel.rug(y = NULL,..., col="black") > }) > > ## explicitly omit y from the panel.rug call (same as above): > xyplot(y ~ foo, > panel = function(y,...) { ## x is in ... arguments > panel.xyplot(y,..., col = "red") > panel.rug(..., col="black")## y omitted > }) > > ## only pass down x explicitly and omit y > xyplot(y ~ foo, > panel = function(x,...) { ## y is in ... arguments > panel.xyplot(x,..., col = "red") > panel.rug(x, col="black") ## only x argument is passed > }) > > Cheers, > > 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 5, 2019 at 10:04 AM Christopher W Ryan <cryan at binghamton.edu> > wrote: > >> Thanks Bert. So my lesson here is that I have to "feed" "x" to all my >> panel functions "upstream" from my panel.rug()? >> >> --Chris Ryan >> >> On Tue, Nov 5, 2019 at 12:28 PM Bert Gunter <bgunter.4567 at gmail.com> >> wrote: >> >>> Here's how you pass an argument down to the panel function. >>> >>> foo <- runif(30,0,5) >>> y <- rnorm(30, mean = 10) >>> xyplot(y~foo, >>> panel = function(x,...) { >>> panel.xyplot(x,..., col = "red") >>> panel.rug(x, col="black") >>> }) >>> >>> Cheers, >>> Bert >>> >>> >>> 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 5, 2019 at 8:41 AM Christopher W Ryan <cryan at binghamton.edu> >>> wrote: >>> >>>> The following produces a scatterplot with rugs on both the vertical and >>>> horizontal axes. >>>> >>>> library(dplyr) >>>> library(stringr) >>>> library(lattice) >>>> library(latticeExtra) >>>> ## ..... >>>> xyplot(scheduleInterval ~ calledForApptDate, data = dd.2, xlab = "Date >>>> patient called for appointment", ylab = "Days in the future that patient >>>> was scheduled", >>>> panel = function(...) { >>>> panel.xyplot(..., col = "red") >>>> panel.smoother(..., span = 0.9, se = FALSE) >>>> panel.rug(...) >>>> }) >>>> >>>> I'd like a rug to appear only on the horizontal axis. None of the >>>> following seem to be the correct syntax: >>>> >>>> panel.rug(..., y = NULL) >>>> panel.rug(..., y = FALSE) >>>> panel.rug(x) >>>> panel.rug(x = ...) >>>> >>>> This does the job: >>>> >>>> xyplot(scheduleInterval ~ calledForApptDate, data = dd.2, xlab = "Date >>>> patient called for appointment", ylab = "Days in the future that patient >>>> was scheduled", >>>> panel = function(...) { >>>> panel.xyplot(..., col = "red") >>>> panel.smoother(..., span = 0.9, se = FALSE) >>>> panel.rug(x = dd.2$calledForApptDate) >>>> }) >>>> >>>> but seems inadvisable. Shouldn't I be making use of ... for passing >>>> arguments through to the panel.rug() function? Specifying a variable >>>> in a >>>> dataframe by name isn't generalizable. >>>> >>>> Thanks. >>>> >>>> --Chris Ryan >>>> >>>> [[alternative HTML version deleted]] >>>> >>>> ______________________________________________ >>>> R-help at 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]]