【问题标题】:How to manually colour strips using function "useOuterStrips" in package "latticeExtra"?如何使用“latticeExtra”包中的“useOuterStrips”函数手动着色条带?
【发布时间】:2015-11-02 13:44:14
【问题描述】:

我想做一个与下面非常相似的情节。但是我想为每个条带使用不同的颜色,例如sub1 为绿色,sub2 为蓝色,sub3 为红色......(所有 seas1 到 seas4 都相同)。到目前为止,我还没有成功。

我在这里查看了这篇文章:change background and text of strips associated to muliple panels in R / lattice,但我没有设法对其进行修改以实现我想要的。我认为这是由于使用了函数useOuterStrips

我使用 R 版本 3.2.0、latticeExtra_0.6-26 和 lattice_0.20-31 使用以下代码制作了该图:

require(lattice)
require(latticeExtra)
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
mydat <- data.frame(response=rnorm(400,mean=1),
                p = factor(sample(rep(1:4,each=100))),
                sub = factor(rep(sprintf("sub%i",1:4),each=100)),
                seas=factor(rep(sprintf("seas%i",1:4),100)))
useOuterStrips(bwplot(response~factor(p)|factor(sub)+factor(seas),
                  data=mydat,par.settings = list(strip.background=list(col = c("skyblue","gold"))),
                  fill = cbPalette,xlab="xlab",ylab="ylab"))

任何帮助将不胜感激!

【问题讨论】:

    标签: r lattice


    【解决方案1】:

    为了将来参考,这可以通过指定处理左侧条带的第二个函数来实现。请参阅?useOuterStrips 中的参数strip.left

    建立在上述SO post之上,

    ## set strip background colors
    cbPalette = c(
      "#999999", "#E69F00", "#56B4E9", "#009E73" # top
      , "#F0E442", "#0072B2", "#D55E00", "#CC79A7" # left
    )
    
    ## define core strip function
    myStripStyle = function(which.panel, factor.levels, col, ...) {
      panel.rect(
        0, 0, 1, 1
        , col = col[which.panel]
        , border = 1
      )
      panel.text(
        x = 0.5
        , y = 0.5
        , lab = factor.levels[which.panel]
        , ...
      )
    }
    
    ## and convenience functions for top ..
    myStripStyleTop = function(which.panel, factor.levels, ...) {
      myStripStyle(
        which.panel
        , factor.levels
        , col = cbPalette[1:4]
      )
    }
    
    ## .. and left strips
    myStripStyleLeft = function(which.panel, factor.levels, ...) {
      myStripStyle(
        which.panel
        , factor.levels
        , col = cbPalette[5:8]
        , srt = 90 # and other arguments passed to `panel.text()`
      )
    }
    
    ## assemble plot
    useOuterStrips(
      bwplot(
        response ~ factor(p) | factor(sub) + factor(seas)
        , data = mydat
        , fill = cbPalette
      )
      , strip = myStripStyleTop
      , strip.left = myStripStyleLeft
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-03
      • 2014-08-22
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多