Skip to content

How to overlay lines and rectangles in lattice plot key

8 messages · Sebastien Bihorel, David Winsemius, Richard M. Heiberger +1 more

#
Hi,

I would like to create a custom key for a lattice xyplot in which line 
elements are displayed on top of rectangle elements. In the example code 
below, the lines and rectangles are shown side by side (the legend 
itself is meaningless, but that is not the point). Is there a way to 
overlay these key elements (but not the text)?

Thanks

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
   iris,
   type = c("p", "r"),
   jitter.x = TRUE,
   jitter.y = TRUE,
   factor = 5,
   key = list(
     column=4,
     text=list(lab=letters[1:4]),
     lines=list(col=1:4, pch=1:4, type='b'),
     rectangles=list(col=1:4, alpha=0.25, border=FALSE)
   )
)
#
I'm not seeing the result that you describe. Attached is the pdf that comes from:

 pdf(); print( xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
 iris,
 type = c("p", "r"),
 jitter.x = TRUE,
 jitter.y = TRUE,
 factor = 5,
 key = list(
   column=4,
   text=list(lab=letters[1:4]),
   lines=list(col=1:4, pch=1:4, type='b'),
   rectangles=list(col=1:4, alpha=0.25, border=FALSE)
 )
) ); dev.off()

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Rplots.pdf
Type: application/pdf
Size: 10022 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20161228/4a4ed380/attachment.pdf>
-------------- next part --------------
#
Yes, but it will probably require work.  I think you will need to
write a grob that does what you want
and then use the grob in a legend statement in the xyplot.

Start with the 'legend' argument to xyplot (about line 940 in ?xyplot).
You will probably need to work directly with grid functions and will
find Paul Murrell's book very helpful.
https://www.crcpress.com/R-Graphics/Murrell/p/book/9781584884866

Rich

On Wed, Dec 28, 2016 at 9:50 PM, sbihorel
<Sebastien.Bihorel at cognigencorp.com> wrote:
#
Hi

Is this something like what you are looking for?
I have put it on the right and heights etc are just a quick guess. (all i
have time for)
It may be a little too complicated for what you want but I thought  of this
first based on your description rather than the plot.

library(grid)
library(lattice)

# legend
XY04.glay <-
grid.layout(nrow = 8,
            ncol = 2,
            heights = unit(rep(1, 2), rep("cm", 2)),
            widths  = unit(c(0.4, 0.8),
                           c("in","in")),
            just = "centre")

XY04.fmG <- frameGrob(layout = XY04.glay)

k <-0
for (j in seq(1,8,2)){
  k = k+1
  
  XY04.fmG <-
  placeGrob(XY04.fmG, textGrob(lab = letters[1:4][k],
                             just = 0,
                             gp = gpar(cex = 0.8)), row = j, col = 1)
  XY04.fmG <-
  placeGrob(XY04.fmG, linesGrob(c(0.2,0.8),  c(0.5, 0.5),
                              gp = gpar(col = c(1:4)[k])), row = j, col = 2)
  XY04.fmG <-
  placeGrob(XY04.fmG, pointsGrob(c(0.2,0.8),  c(0.5, 0.5),
                              pch = j,
                              gp = gpar(cex = 0.7, col = c(1:4)[k])), row =
j, col = 2)
}
k = 0
for (j in seq(2,8,2)){
  k = k+1
  XY04.fmG <-
  placeGrob(XY04.fmG, rectGrob(width = 0.6,
                               gp = gpar(col=k,
                                         alpha = 0.25,
                                         fill = k)), row = j, col = 2)

}

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
   iris,
   type = c("p", "r"),
   jitter.x = TRUE,
   jitter.y = TRUE,
   factor = 5,
   legend = list(right = list(fun = XY04.fmG))
)


Have a look at https://stat.ethz.ch/pipermail/r-help/2005-April/069459.html
and the following emails on the thread.

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of sbihorel
Sent: Thursday, 29 December 2016 13:51
To: r-help at r-project.org
Subject: [R] How to overlay lines and rectangles in lattice plot key

Hi,

I would like to create a custom key for a lattice xyplot in which line 
elements are displayed on top of rectangle elements. In the example code 
below, the lines and rectangles are shown side by side (the legend 
itself is meaningless, but that is not the point). Is there a way to 
overlay these key elements (but not the text)?

Thanks

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
   iris,
   type = c("p", "r"),
   jitter.x = TRUE,
   jitter.y = TRUE,
   factor = 5,
   key = list(
     column=4,
     text=list(lab=letters[1:4]),
     lines=list(col=1:4, pch=1:4, type='b'),
     rectangles=list(col=1:4, alpha=0.25, border=FALSE)
   )
)

______________________________________________
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.
#
I think the intended appearance is closer to this

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
  iris,
  type = c("p", "r"),
  jitter.x = TRUE,
  jitter.y = TRUE,
  factor = 5,
  key = list(between=c(-4.5),
    column=4,
    text=list(lab=paste0("       ", letters[1:4], "           ")),
    lines=list(col=1:4, pch=1:4, type='b'),
    rectangles=list(col=1:4, alpha=0.25, border=FALSE)
  )
)

I have the partial overlap of the rectangles and the line-point objects.
They don't align correctly.  I think even more complex grid usage is needed than
Duncan provided.

Rich
On Thu, Dec 29, 2016 at 12:16 AM, Duncan Mackay <dulcalma at bigpond.com> wrote:
#
Hi 

It makes it easier

library(grid)
library(lattice)

# legend
XY04.glay <-
grid.layout(nrow = 4,
            ncol = 2,
            heights = unit(rep(1, 2), rep("cm", 2)),
            widths  = unit(c(0.4, 0.8),
                           c("in","in")),
            just = "centre")

XY04.fmG <- frameGrob(layout = XY04.glay)

k <-0
for (j in seq_len(4)){

  XY04.fmG <-
  placeGrob(XY04.fmG, textGrob(lab = letters[j],
                             just = 0,
                             gp = gpar(cex = 0.8)), row = j, col = 1)
  XY04.fmG <-
  placeGrob(XY04.fmG, rectGrob(width = 0.6,
                               gp = gpar(col=j,
                                         alpha = 0.25,
                                         fill = j)), row = j, col = 2)
  XY04.fmG <-
  placeGrob(XY04.fmG, linesGrob(c(0.2,0.8),  c(0.5, 0.5),
                              gp = gpar(col = j)), row = j, col = 2)
  XY04.fmG <-
  placeGrob(XY04.fmG, pointsGrob(x = unit(1, "cm"), y = unit(0.5, "npc"),
                              pch = j,
                              #width = unit(2, "cm"),
                              gp = gpar(cex = 0.7, col = j)), row = j, col =
2)
}

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
   iris,
   type = c("p", "r"),
   jitter.x = TRUE,
   jitter.y = TRUE,
   factor = 5,
   legend = list(right = list(fun = XY04.fmG))
)

This gives 1 point instead of 2 or 3.
The OP needs to change the format and fix the heights of the rectangles etc.
I also fixed the colour vectors had j instead of k


Duncan

-----Original Message-----
From: Duncan Mackay [mailto:dulcalma at bigpond.com] 
Sent: Thursday, 29 December 2016 16:17
To: R
Subject: RE: [R] How to overlay lines and rectangles in lattice plot key

Hi

Is this something like what you are looking for?
I have put it on the right and heights etc are just a quick guess. (all i
have time for)
It may be a little too complicated for what you want but I thought  of this
first based on your description rather than the plot.

library(grid)
library(lattice)

# legend
XY04.glay <-
grid.layout(nrow = 8,
            ncol = 2,
            heights = unit(rep(1, 2), rep("cm", 2)),
            widths  = unit(c(0.4, 0.8),
                           c("in","in")),
            just = "centre")

XY04.fmG <- frameGrob(layout = XY04.glay)

k <-0
for (j in seq(1,8,2)){
  k = k+1
  
  XY04.fmG <-
  placeGrob(XY04.fmG, textGrob(lab = letters[1:4][k],
                             just = 0,
                             gp = gpar(cex = 0.8)), row = j, col = 1)
  XY04.fmG <-
  placeGrob(XY04.fmG, linesGrob(c(0.2,0.8),  c(0.5, 0.5),
                              gp = gpar(col = c(1:4)[k])), row = j, col = 2)
  XY04.fmG <-
  placeGrob(XY04.fmG, pointsGrob(c(0.2,0.8),  c(0.5, 0.5),
                              pch = j,
                              gp = gpar(cex = 0.7, col = c(1:4)[k])), row =
j, col = 2)
}
k = 0
for (j in seq(2,8,2)){
  k = k+1
  XY04.fmG <-
  placeGrob(XY04.fmG, rectGrob(width = 0.6,
                               gp = gpar(col=k,
                                         alpha = 0.25,
                                         fill = k)), row = j, col = 2)

}

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
   iris,
   type = c("p", "r"),
   jitter.x = TRUE,
   jitter.y = TRUE,
   factor = 5,
   legend = list(right = list(fun = XY04.fmG))
)


Have a look at https://stat.ethz.ch/pipermail/r-help/2005-April/069459.html
and the following emails on the thread.

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of sbihorel
Sent: Thursday, 29 December 2016 13:51
To: r-help at r-project.org
Subject: [R] How to overlay lines and rectangles in lattice plot key

Hi,

I would like to create a custom key for a lattice xyplot in which line 
elements are displayed on top of rectangle elements. In the example code 
below, the lines and rectangles are shown side by side (the legend 
itself is meaningless, but that is not the point). Is there a way to 
overlay these key elements (but not the text)?

Thanks

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
   iris,
   type = c("p", "r"),
   jitter.x = TRUE,
   jitter.y = TRUE,
   factor = 5,
   key = list(
     column=4,
     text=list(lab=letters[1:4]),
     lines=list(col=1:4, pch=1:4, type='b'),
     rectangles=list(col=1:4, alpha=0.25, border=FALSE)
   )
)

______________________________________________
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.
#
I forgot to change the xyplot for the colours; if you want the same colours
in the key you need to use the par.settings argument or set the settings for
the device.

the xyplot becomes

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
   iris,
   par.settings = list(superpose.symbol = list(col = 1:4,
                                               cex = 1,
                                               pch = 1:4),
                       superpose.line = list(col = 1:4)
                  ),
   type = c("p", "r"),
   jitter.x = TRUE,
   jitter.y = TRUE,
   factor = 5,
   legend = list(right = list(fun = XY04.fmG))
)
 
Duncan

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Duncan
Mackay
Sent: Thursday, 29 December 2016 18:21
To: R
Subject: Re: [R] How to overlay lines and rectangles in lattice plot key

Hi 

It makes it easier

library(grid)
library(lattice)

# legend
XY04.glay <-
grid.layout(nrow = 4,
            ncol = 2,
            heights = unit(rep(1, 2), rep("cm", 2)),
            widths  = unit(c(0.4, 0.8),
                           c("in","in")),
            just = "centre")

XY04.fmG <- frameGrob(layout = XY04.glay)

k <-0
for (j in seq_len(4)){

  XY04.fmG <-
  placeGrob(XY04.fmG, textGrob(lab = letters[j],
                             just = 0,
                             gp = gpar(cex = 0.8)), row = j, col = 1)
  XY04.fmG <-
  placeGrob(XY04.fmG, rectGrob(width = 0.6,
                               gp = gpar(col=j,
                                         alpha = 0.25,
                                         fill = j)), row = j, col = 2)
  XY04.fmG <-
  placeGrob(XY04.fmG, linesGrob(c(0.2,0.8),  c(0.5, 0.5),
                              gp = gpar(col = j)), row = j, col = 2)
  XY04.fmG <-
  placeGrob(XY04.fmG, pointsGrob(x = unit(1, "cm"), y = unit(0.5, "npc"),
                              pch = j,
                              #width = unit(2, "cm"),
                              gp = gpar(cex = 0.7, col = j)), row = j, col =
2)
}

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
   iris,
   type = c("p", "r"),
   jitter.x = TRUE,
   jitter.y = TRUE,
   factor = 5,
   legend = list(right = list(fun = XY04.fmG))
)

This gives 1 point instead of 2 or 3.
The OP needs to change the format and fix the heights of the rectangles etc.
I also fixed the colour vectors had j instead of k


Duncan

-----Original Message-----
From: Duncan Mackay [mailto:dulcalma at bigpond.com] 
Sent: Thursday, 29 December 2016 16:17
To: R
Subject: RE: [R] How to overlay lines and rectangles in lattice plot key

Hi

Is this something like what you are looking for?
I have put it on the right and heights etc are just a quick guess. (all i
have time for)
It may be a little too complicated for what you want but I thought  of this
first based on your description rather than the plot.

library(grid)
library(lattice)

# legend
XY04.glay <-
grid.layout(nrow = 8,
            ncol = 2,
            heights = unit(rep(1, 2), rep("cm", 2)),
            widths  = unit(c(0.4, 0.8),
                           c("in","in")),
            just = "centre")

XY04.fmG <- frameGrob(layout = XY04.glay)

k <-0
for (j in seq(1,8,2)){
  k = k+1
  
  XY04.fmG <-
  placeGrob(XY04.fmG, textGrob(lab = letters[1:4][k],
                             just = 0,
                             gp = gpar(cex = 0.8)), row = j, col = 1)
  XY04.fmG <-
  placeGrob(XY04.fmG, linesGrob(c(0.2,0.8),  c(0.5, 0.5),
                              gp = gpar(col = c(1:4)[k])), row = j, col = 2)
  XY04.fmG <-
  placeGrob(XY04.fmG, pointsGrob(c(0.2,0.8),  c(0.5, 0.5),
                              pch = j,
                              gp = gpar(cex = 0.7, col = c(1:4)[k])), row =
j, col = 2)
}
k = 0
for (j in seq(2,8,2)){
  k = k+1
  XY04.fmG <-
  placeGrob(XY04.fmG, rectGrob(width = 0.6,
                               gp = gpar(col=k,
                                         alpha = 0.25,
                                         fill = k)), row = j, col = 2)

}

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
   iris,
   type = c("p", "r"),
   jitter.x = TRUE,
   jitter.y = TRUE,
   factor = 5,
   legend = list(right = list(fun = XY04.fmG))
)


Have a look at https://stat.ethz.ch/pipermail/r-help/2005-April/069459.html
and the following emails on the thread.

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of sbihorel
Sent: Thursday, 29 December 2016 13:51
To: r-help at r-project.org
Subject: [R] How to overlay lines and rectangles in lattice plot key

Hi,

I would like to create a custom key for a lattice xyplot in which line 
elements are displayed on top of rectangle elements. In the example code 
below, the lines and rectangles are shown side by side (the legend 
itself is meaningless, but that is not the point). Is there a way to 
overlay these key elements (but not the text)?

Thanks

xyplot(Sepal.Length + Sepal.Width ~ Petal.Length + Petal.Width,
   iris,
   type = c("p", "r"),
   jitter.x = TRUE,
   jitter.y = TRUE,
   factor = 5,
   key = list(
     column=4,
     text=list(lab=letters[1:4]),
     lines=list(col=1:4, pch=1:4, type='b'),
     rectangles=list(col=1:4, alpha=0.25, border=FALSE)
   )
)

______________________________________________
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.

______________________________________________
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.
#
Thanks to all the persons who replied,

I was hoping for a quick "grid-free" solution but I guess it is not 
Christmas time anymore :D

Grid coding it is.
On 12/29/2016 12:13 AM, Richard M. Heiberger wrote: