[R] How to overlay lines and rectangles in lattice plot key

Richard M. Heiberger rmh at temple.edu
Thu Dec 29 06:53:48 CET 2016


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



More information about the R-help mailing list