[R] lattice key inside panel

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Feb 3 10:16:51 CET 2010


On Wed, Feb 3, 2010 at 10:50 AM, Santosh <santosh2005 at gmail.com> wrote:
> Dear R-experts..
>
> I am trying to generate legend inside panels of lattice barplots..
> Following is a sample dataset..
>   Sno DN  EP Y1 Y2
> 1    1 D1 A20 19 19
> 2    1 D1 A50  8  9
> 3    1 D1 A70  3  4
> 4    2 D2 A20 22 22
> 5    2 D2 A50 13 14
> 6    2 D2 A70  6  7
> 7    3 D2 A20 23 23
> 8    3 D2 A50 12 13
> 9    4 D1 A20 19 19
> 10   4 D1 A50 10 10
> 11   4 D1 A70  3  4
> 12   5 D1 A20 28 28
> 13   5 D1 A50 16 16
> 14   5 D1 A70  7  7
> 15   5 D3 A20 24 24
> 16   5 D3 A50 14 14
> 17   5 D3 A70  6  6
> 18   6 D4 A20 15 15
> 19   6 D4 A50  7  8
> 20   6 D4 A70  3  3
> 21   7 D5 A20 19 20
> 22   7 D5 A50  5  6
> 23   7 D5 A70  3  3
> 24   8 D6 A20 26 26
> 25   8 D6 A50 10 10
> 26   9 D7 A20 30 30
> 27   9 D7 A50 10  9
> 28  10 D7 A20 21 22
> 29  10 D7 A50 10 10
> 30  10 D7 A70  4  4
>
> I am trying to generate barplot with legend inside each panel..where the
> rectangular colors indicate the values for "DN". I have not been successful
> in generating labels inside.. what am I missing?

You should have

             draw.key(list(
                           rectangles=list(col=rainbow(6)),
                           text=list(lab='DN')),
                      draw = TRUE,
                      vp = viewport(x = unit(0.5, "npc"), y = unit(0.5, "npc")))

whereas you had

             draw.key(list(
                           rectangles=list(col=rainbow(6)),
                           text=list(lab='DN'),
                           draw = TRUE,
                           vp = viewport(x = unit(0.5, "npc"), y =
unit(0.5, "npc"))))

-Deepayan

>
> barchart(Y1~factor(as.character(EP))|Sno,
>        groups=DN,
>        orgin=0,
>        subscripts=T,
>        panel = function(x,y,groups=groups, ...) {
>          require(grid)
>          panel.barchart(x,y,groups=groups,...)
>          #panel.text(...)
>          draw.key(list(
>          rectangles=list(col=rainbow(6)),
>          text=list(lab='DN'),
>          draw = TRUE,
>           vp = viewport(x = unit(0.5, "npc"), y = unit(0.5, "npc"))))},
>        data=x1,
>        distribute.type=T,
>        layout=c(3,3),
>        scales=list(rot=45,relation="free"))
>
> Thanks,
> Santosh



More information about the R-help mailing list