Dear all,
I posted this on Stack Overflow, but there is no response, so I try it here again.
I'm wondering about how to draw a highlighted rectangle with log y-axis in a trellis/lattice plot. My idea was to use panel.rect(), which works without a log scale:
library(lattice)
set.seed(1)
y <- runif(100, min=1, max=1000)
x <- seq_along(y)
xyplot(y ~ x,
panel=function(x,y,...) {
cpl <- current.panel.limits()
panel.rect(xleft=cpl$xlim[1], ybottom=10,
xright=cpl$xlim[2], ytop=500,
fill="lightgray", border="lightgray", alpha=.6)
panel.xyplot(x,y,...)
},
scales=list(y=list(log=FALSE))
)
However, when log=TRUE, the rectangle disappears.
Are there any ideas? Thanks, Sven
<https://mail.med.uni-heidelberg.de/questions/tagged/,DanaInfo=stackoverflow.com,SSL+r>
panel.rect and log scale in lattice plot
7 messages · Ebert,Timothy Aaron, Ivan Krylov, Deepayan Sarkar +1 more
Have you tried the ggplot2 package? http://www.sthda.com/english/wiki/ggplot2-add-straight-lines-to-a-plot-horizontal-vertical-and-regression-lines Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Garbade, Sven via R-help Sent: Wednesday, March 23, 2022 5:39 AM To: r-help at r-project.org Subject: [R] panel.rect and log scale in lattice plot [External Email] Dear all, I posted this on Stack Overflow, but there is no response, so I try it here again. I'm wondering about how to draw a highlighted rectangle with log y-axis in a trellis/lattice plot. My idea was to use panel.rect(), which works without a log scale: library(lattice) set.seed(1) y <- runif(100, min=1, max=1000) x <- seq_along(y) xyplot(y ~ x, panel=function(x,y,...) { cpl <- current.panel.limits() panel.rect(xleft=cpl$xlim[1], ybottom=10, xright=cpl$xlim[2], ytop=500, fill="lightgray", border="lightgray", alpha=.6) panel.xyplot(x,y,...) }, scales=list(y=list(log=FALSE)) ) However, when log=TRUE, the rectangle disappears. Are there any ideas? Thanks, Sven <https://urldefense.proofpoint.com/v2/url?u=https-3A__mail.med.uni-2Dheidelberg.de_questions_tagged_-2CDanaInfo-3Dstackoverflow.com-2CSSL-2Br&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=FpPsvaC4RAcwCFXTkiSHekD-Kq42a0hxUJvGstcn9J-97cF_sKhux4I2Q2Xoys4N&s=zoK3I2T0bpTMEgeKFzwh3IGQsunArwGKt734uAuYKY4&e= > ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=FpPsvaC4RAcwCFXTkiSHekD-Kq42a0hxUJvGstcn9J-97cF_sKhux4I2Q2Xoys4N&s=Ah56En_K57pgqMo-alPPK_pxyzmHpZt4xF7hO3DDpS4&e= PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=FpPsvaC4RAcwCFXTkiSHekD-Kq42a0hxUJvGstcn9J-97cF_sKhux4I2Q2Xoys4N&s=Sf4-2puTGAtpoZr1CNzG7wyOZFcVptczggv4aoWUji0&e= and provide commented, minimal, self-contained, reproducible code.
Yes, thanks. The figure is for publication, I started with lattice and I don't like to use two different graph types (lattice and ggplot2). When there is no workaround, I re-create all figures with ggplot2. In the meanwhile, I tried "panel.polygon()", which also does not work.
Von: Ebert,Timothy Aaron <tebert at ufl.edu>
Gesendet: Mittwoch, 23. M?rz 2022 11:15 An: Garbade, Sven; r-help at r-project.org Betreff: RE: panel.rect and log scale in lattice plot Have you tried the ggplot2 package? http://www.sthda.com/english/wiki/ggplot2-add-straight-lines-to-a-plot-horizontal-vertical-and-regression-lines Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Garbade, Sven via R-help Sent: Wednesday, March 23, 2022 5:39 AM To: r-help at r-project.org Subject: [R] panel.rect and log scale in lattice plot [External Email] Dear all, I posted this on Stack Overflow, but there is no response, so I try it here again. I'm wondering about how to draw a highlighted rectangle with log y-axis in a trellis/lattice plot. My idea was to use panel.rect(), which works without a log scale: library(lattice) set.seed(1) y <- runif(100, min=1, max=1000) x <- seq_along(y) xyplot(y ~ x, panel=function(x,y,...) { cpl <- current.panel.limits() panel.rect(xleft=cpl$xlim[1], ybottom=10, xright=cpl$xlim[2], ytop=500, fill="lightgray", border="lightgray", alpha=.6) panel.xyplot(x,y,...) }, scales=list(y=list(log=FALSE)) ) However, when log=TRUE, the rectangle disappears. Are there any ideas? Thanks, Sven <https://urldefense.proofpoint.com/v2/url?u=https-3A__mail.med.uni-2Dheidelberg.de_questions_tagged_-2CDanaInfo-3Dstackoverflow.com-2CSSL-2Br&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=FpPsvaC4RAcwCFXTkiSHekD-Kq42a0hxUJvGstcn9J-97cF_sKhux4I2Q2Xoys4N&s=zoK3I2T0bpTMEgeKFzwh3IGQsunArwGKt734uAuYKY4&e= > [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=FpPsvaC4RAcwCFXTkiSHekD-Kq42a0hxUJvGstcn9J-97cF_sKhux4I2Q2Xoys4N&s=Ah56En_K57pgqMo-alPPK_pxyzmHpZt4xF7hO3DDpS4&e= PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=FpPsvaC4RAcwCFXTkiSHekD-Kq42a0hxUJvGstcn9J-97cF_sKhux4I2Q2Xoys4N&s=Sf4-2puTGAtpoZr1CNzG7wyOZFcVptczggv4aoWUji0&e= and provide commented, minimal, self-contained, reproducible code.
On Wed, 23 Mar 2022 09:38:34 +0000
"Garbade, Sven via R-help" <r-help at r-project.org> wrote:
cpl <- current.panel.limits()
If you str() the return value of current.panel.limits() from the panel function with log-scaling enabled, you can see that it contains the logarithm of the y-values, as do the y values themselves. This is consistent with ?xyplot saying:
Note that this is in reality a transformation of the data, not the axes. Other than the axis labeling, using this feature is no different than transforming the data in the formula; e.g., ?scales=list(x = list(log = 2))? is equivalent to ?y ~ log2(x)?.
...although it could be more explicit. If you take a logarithm of 10 and 500, lrect() should be able to produce a rectangle in the right place.
Best regards, Ivan
thanks, this do the trick!
xyplot(y ~ x,
panel=function(x,y, ...) {
cpl <- current.panel.limits()
yseq <- log(c(100,1000))
panel.rect(xleft=cpl$xlim[1],
ybottom=yseq[1],
xright=cpl$xlim[2],
ytop=yseq[2],
fill="lightgray", border="lightgray", alpha=.6)
panel.xyplot(x,y,...)
},
scales=list(y=list(log=2))
)
Best Regards, Sven
Von: Ivan Krylov <krylov.r00t at gmail.com>
Gesendet: Mittwoch, 23. M?rz 2022 11:33 An: Garbade, Sven via R-help Cc: Garbade, Sven Betreff: Re: [R] panel.rect and log scale in lattice plot On Wed, 23 Mar 2022 09:38:34 +0000 "Garbade, Sven via R-help" <r-help at r-project.org> wrote: > cpl <- current.panel.limits() If you str() the return value of current.panel.limits() from the panel function with log-scaling enabled, you can see that it contains the logarithm of the y-values, as do the y values themselves. This is consistent with ?xyplot saying: >> Note that this is in reality a transformation of the data, not the >> axes. Other than the axis labeling, using this feature is no >> different than transforming the data in the formula; e.g., >> ?scales=list(x = list(log = 2))? is equivalent to ?y ~ log2(x)?. ...although it could be more explicit. If you take a logarithm of 10 and 500, lrect() should be able to produce a rectangle in the right place. -- Best regards, Ivan
On Wed, Mar 23, 2022 at 4:08 PM Ivan Krylov <krylov.r00t at gmail.com> wrote:
On Wed, 23 Mar 2022 09:38:34 +0000 "Garbade, Sven via R-help" <r-help at r-project.org> wrote:
cpl <- current.panel.limits()
If you str() the return value of current.panel.limits() from the panel function with log-scaling enabled, you can see that it contains the logarithm of the y-values, as do the y values themselves. This is consistent with ?xyplot saying:
Note that this is in reality a transformation of the data, not the axes. Other than the axis labeling, using this feature is no different than transforming the data in the formula; e.g., ?scales=list(x = list(log = 2))? is equivalent to ?y ~ log2(x)?.
...although it could be more explicit. If you take a logarithm of 10 and 500, lrect() should be able to produce a rectangle in the right place.
Right, "log scales" in lattice simply transform the data, unlike in traditional graphics. If it helps, I could change current.panel.limits() to return information on whether the data were transformed. For now, a roundabout way to detect this inside the panel function is to use trellis.last.object()$y.scales$log etc. Best, -Deepayan
-- Best regards, Ivan
______________________________________________ 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.
using trellis.last.object()$y.scales$log helps a lot, thanks! I now check whether data were transformed and apply an appropriate transformation if needed. Maybe it helps when current.panel.limit() has information about transformations, so someone doesn't need to call 'trellis.last.object()', too.
Von: Deepayan Sarkar <deepayan.sarkar at gmail.com>
Gesendet: Mittwoch, 23. M?rz 2022 12:05 An: Ivan Krylov Cc: Garbade, Sven via R-help; Garbade, Sven Betreff: Re: [R] panel.rect and log scale in lattice plot On Wed, Mar 23, 2022 at 4:08 PM Ivan Krylov <krylov.r00t at gmail.com> wrote: > > On Wed, 23 Mar 2022 09:38:34 +0000 > "Garbade, Sven via R-help" <r-help at r-project.org> wrote: > > > cpl <- current.panel.limits() > > If you str() the return value of current.panel.limits() from the panel > function with log-scaling enabled, you can see that it contains the > logarithm of the y-values, as do the y values themselves. This is > consistent with ?xyplot saying: > > >> Note that this is in reality a transformation of the data, not the > >> axes. Other than the axis labeling, using this feature is no > >> different than transforming the data in the formula; e.g., > >> ?scales=list(x = list(log = 2))? is equivalent to ?y ~ log2(x)?. > > ...although it could be more explicit. > > If you take a logarithm of 10 and 500, lrect() should be able to > produce a rectangle in the right place. Right, "log scales" in lattice simply transform the data, unlike in traditional graphics. If it helps, I could change current.panel.limits() to return information on whether the data were transformed. For now, a roundabout way to detect this inside the panel function is to use trellis.last.object()$y.scales$log etc. Best, -Deepayan > > -- > Best regards, > Ivan > > ______________________________________________ > 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.