简体   繁体   中英

Combine different key element types (rectangles, lines, ...) in lattice plot in R

At first I thought this would be trivial, but I could not figure out how to combine rectangles with lines in the legend of a lattice plot. Consider the following example:

library(latticeExtra)

xyplot(rnorm(10) ~ 1:10, 
       key=list(rectangles=list(size=2, border=F), 
                text=list(c("Zero", "One", "Two"), col="black"), 
                col=c("black", "lightgrey", "darkgrey"), divide=1, columns=1,
                x=0.01, y=0.95, corner=c(0,1) ),  
                     panel=function(x,...){
                       panel.abline(v=3, lty="dashed")        
                       panel.xblocks(x,x>5, col="lightgrey")
                       panel.xblocks(x,x>7, col="darkgrey")
                       panel.xyplot(x, ...)        }      )

Instead of 3 rectangles, I would like to have 2 rectangles filled with the appropriate colours, and one dashed line above or below these two rectangles. If I provide a lines argument, then both lines and rectangles will be drawn for all elements (ie there will be 2 rectangles and 2 lines simultaneously next to each other).

How can I set up the legend key so that I get this mix of "symbols"? That is, how can I get one dashed line and two rectangles with the appropriate text and colours?

Any help is greatly appreciated. My apologies if this is trival: Please help me see the obvious! :)

This is a rather late answer but it is something I still do. One approach is to use auto.key in the function and then modify the lattice object with update(obj, key = newKey) . A more general approach, as suggested by @josh-obrien, is to use the grid functions that under lattice. However, this typically requires empirical tweaking of the coordinates as can be seen in the need to use 3 decimal places of precision to place the dashed lines.

# relative position may be sensitive to absolute sizes
  library(latticeExtra)
  dev.new(width = 5, height = 5)
  set.seed(1234)

# same code as in question, re-written a little bit
# using "transparent" for the 1st of the three rectangles
# using a grid call in the panel function to place the dashed line
  xyplot(rnorm(10) ~ 1:10, 
    key = list(rectangles = list(size = 2, border = FALSE, 
        col = c("transparent", "lightgrey", "darkgrey")),
      text = list(c("Zero", "One", "Two"), col = "black"), 
        columns = 1, corner = c(0.01, 0.95)),  
    panel = function(x,...) {
      panel.abline(v = 3, lty = "dashed")        
      panel.xblocks(x, x > 5, col = "lightgrey")
      panel.xblocks(x, x > 7, col = "darkgrey")
      panel.xyplot(x, ...)

      grid::grid.lines(c(0.04, 0.07), c(0.935, 0.935), 
        gp = gpar(lty = "dashed", col = "black"))

    }
  )
[![plot with combined elements in legend][1]][1]


  [1]: https://i.stack.imgur.com/K7AJN.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM