change background and text of strips associated to multiple panels in R / lattice

前端 未结 2 1772
灰色年华
灰色年华 2020-11-28 13:47

The following is the example I work on.

require(lattice)
data(barley)
xyplot(yield ~ year | site, data = barley)

2条回答
  •  鱼传尺愫
    2020-11-28 14:21

    Here's a clean and easily customizable solution.

    myStripStyle(), the function that is passed in to the strip= argument of xyplot() uses the counter variable which.panel to select colors and also the value of factor.levels for the panel that's currently being plotted.

    If you want to play around with the settings, just put a browser() somewhere inside the definition of myStripStyle() and have at it!

    bgColors <- c("black", "green4", "blue", "red", "purple", "yellow")
    txtColors <- c("white", "yellow", "white", "white", "green", "red")
    
    # Create a function to be passed to "strip=" argument of xyplot
    myStripStyle <- function(which.panel, factor.levels, ...) {
        panel.rect(0, 0, 1, 1,
                   col = bgColors[which.panel],
                   border = 1)
        panel.text(x = 0.5, y = 0.5,
                   font=2,
                   lab = factor.levels[which.panel],
                   col = txtColors[which.panel])
    }    
    xyplot(yield ~ year | site, data = barley, strip=myStripStyle)
    

    enter image description here

提交回复
热议问题