Skip to content

[lattice xyplot] Help needed in help in customizing the panel.abline() function

7 messages · Girish A.R., Dieter Menne, Felix Andrews

#
Hi folks,

I need some help in customizing the abline() function to be used in a
lattice plot. I have attached a reproducible example below. 

I need help in the following snippet:
disc <- xyplot(cnt_gt50pct_disc ~ week_num|sku_num, data=DF,type =
"h",lwd=2,panel = function(...) {
           panel.abline(v = 8, lty = 2)
           panel.xyplot(...)
       })

Is there a way I can give panel.abline() input from a which.max() function?
Essentially I need the vertical line to be drawn at the week_num
corresponding to the max (cnt_gt50pct_disc). 

Thanks in advance,

-Girish 

===========================================
Lines <- "sku_num week_num    pct_inv_left    cnt_gt50pct_disc
1   1   99.88   47
1   2   99.54   109
1   3   98.7    260
1   4   97.83   202
1   5   96.53   389
1   6   94.11   450
1   7   90.42   459
1   8   86.63   448
1   9   83.39   411
1   10  77  478
1   11  71.65   476
1   12  67.3    463
1   13  62.45   472
1   14  52.47   488
1   15  40.86   486
1   16  31.34   484
1   17  23.2    472
1   18  17  458
1   19  12.66   423
1   20  10.18   364
1   21  7.6 343
1   22  3.09    343
1   23  1.05    211
2   1   99.94   30
2   2   99.4    151
2   3   98.85   146
2   4   97.92   274
2   5   97.03   204
2   6   95.59   378
2   7   92.81   452
2   8   89.07   470
2   9   85.11   454
2   10  81.68   421
2   11  75.34   479
2   12  70.05   476
2   13  66.11   456
2   14  61.85   465
2   15  53.2    485
2   16  42.75   486
2   17  33.58   481
2   18  25  477
2   19  18.13   450
2   20  12.97   416
2   21  10.03   343
2   22  7.03    293
2   23  2.33    283
2   24  0.77    116
"

DF <- read.table(con<- textConnection(Lines), skip = 1);
names(DF) <- scan(textConnection(Lines), what = "", nlines = 1) ;
close(con); 

require(latticeExtra)
DF$sku_num <- as.factor(DF$sku_num)
disc <- xyplot(cnt_gt50pct_disc ~ week_num|sku_num, data=DF,type =
"h",lwd=2,panel = function(...) {
           panel.abline(v = 8, lty = 2)
           panel.xyplot(...)
       })
sales <- xyplot(pct_inv_left  ~ week_num|sku_num, data=swtop16,type =
"l",lwd=2,panel = function(...) {
           panel.abline(h = 75, lty = 2)
           panel.xyplot(...)
       })
doubleYScale(disc, sales, style1 = 0, style2 = 2, add.ylab2 = TRUE,text =
c("# stores with gt 50pct disc", "% Unsold"))
#
Sorry, just realized that there was a typo in the following code of my
original post. The correct code is as shown below (corrected 'data=swtop16'
to 'data=DF'):

sales <- xyplot(pct_inv_left  ~ week_num|sku_num, data=DF,type =
"l",lwd=2,panel = function(...) {
           panel.abline(h = 75, lty = 2)
           panel.xyplot(...)
       }) 

Thanks,
-Girish
#
Girish A.R. wrote:
If it is really a constant not dependent on grouping, you could compute it
in the parent; or, better probably, compute it in panel.abline()

Dieter

.. Your data here ...

maxWeek = DF$week_num[which.max (DF$cnt_gt50pct_disc)]

sales <- xyplot(pct_inv_left  ~ week_num|sku_num, data=DF,type =
"l",lwd=2,panel = function(...) {
           panel.abline(h = 75, lty = 2)
           panel.abline(v = maxWeek, lty = 2)
           panel.xyplot(...)
       })
print(sales)
#
Thanks for the reply, Dieter.

I'm sorry I should have made it clear in my original post - the number
(output of which.max()) IS dependent on the grouping..

Thanks,
-Girish
1 day later
#
Hi Girish,

Try this:

disc <- xyplot(cnt_gt50pct_disc ~ week_num|sku_num, data=DF,type =
"h",lwd=2,panel = function(x, y, ...) {
          panel.abline(v = x[which.max(y)], lty = 2)
          panel.xyplot(x, y, ...)
      })

-Felix
On 9 December 2010 17:35, Girish A.R. <garamach at gmail.com> wrote: