【问题标题】:R: How do I set two titles (main and sub-title) in each lattice xyplot?R:如何在每个格子 xyplot 中设置两个标题(主标题和副标题)?
【发布时间】:2020-05-19 15:40:08
【问题描述】:

我的 R 数据框中有一个示例数据:

d <- read.table(text = 
"Grp     Var    Col1  Col2    Col3    Sub_grp
grp_1   8      46.8  50.0   50.6    A
grp_1   16     95.6  47.4   48.0    A
grp_1   24     45.1  45.6   46.4    A
grp_1   32     68.8  44.3   58.2    A
grp_1   40     44.6  52.2   44.3    A
grp_1   48     86.5  42.2   68.6    A
grp_2   40     63.2  95.6   63.0    B
grp_2   60     66.7  67.5   65.6    B
grp_2   80     69.6  70.7   67.9    B
grp_2   100    71.9  73.4   69.3    B
grp_2   120    73.8  75.7   48.0    B
grp_3   500    51.9  50.0   50.5    C
grp_3   1000   65.5  53.0   53.4    C
grp_3   5000   61.2  99.0   59.9    C
grp_3   10000  80.1  63.0   62.8    C
grp_3   30000  25.9  33.8   14.2    C
  ", header=T
)

下面的代码可以正常工作并像这样绘制数据:

xyplot(
  Col1 + Col2 + Col3 ~ Var | Grp,
  data=d,
  superpose=T,
  as.table=TRUE,
  col=c("#cc0000","#008000", "#0073e6"),
  lwd=1.5, ylim = c(0,120),
  ylab = list(label=expression("My Values"), fontsize=15),
  xlab = list(label="Var",fontsize=15),
  key=list(
    text  = list(c("Col1", "Col2", "Col3")),
    lines = list( lwd=1.5, col=c("#cc0000","#008000", "#0073e6")),
    corner = c(1, 0),
    x = 0.90, 
    y = 0.17),
  par.settings = list(strip.background=list(col="white")),
  scales=list(cex=1.2,x=list(relation="free")),
  type=c("l","g")
)

现在,我想在每个图中添加列 Sub_grp 作为子标题,这样它就应该有两个标题(Grp 和 Sub_grp),而不是一个标题(grp)。我在其中一个相关问题上尝试了建议here,并更改了行:

Col1 + Col2 + Col3 ~ Var | Sub_grp+Grp

在上面的代码中,我的情节是这样的。有关如何解决此问题的任何建议?

【问题讨论】:

    标签: r lattice


    【解决方案1】:

    由于您的 Grp 和 Sub_grp 映射 1:1,您可以尝试

    Col1 + Col2 + Col3 ~ Var | paste(Grp,Sub_grp,sep=" / ")

    还可以考虑指定 layout=c(3,1) 来强制布局,以便您手动放置的键位于您想要的位置。

    更新以将 Grp 和 Sub_grp 放在一个框中的不同行上。

    使用sep="\n" 在两部分之间添加换行符,使用par.strip.text 控制框大小以适应两行。

    xyplot(
      Col1 + Col2 + Col3 ~ Var | paste(Grp,Sub_grp,sep="\n"),
      data=d,
      superpose=T,
      as.table=TRUE,
      col=c("#cc0000","#008000", "#0073e6"),
      lwd=1.5, ylim = c(0,120),
      ylab = list(label=expression("My Values"), fontsize=15),
      xlab = list(label="Var",fontsize=15),
      key=list(
        text  = list(c("Col1", "Col2", "Col3")),
        lines = list( lwd=1.5, col=c("#cc0000","#008000", "#0073e6")),
        corner = c(1, 0),
        x = 0.90, 
        y = 0.17),
      par.settings = list(strip.background=list(col="white")),
      par.strip.text=list(lines=2),
      scales=list(cex=1.2,x=list(relation="free")),
      type=c("l","g"),layout=c(3,1)
    )
    

    更新 2

    如果你很挑剔,想要标题和字幕更好地对齐,你可以用空格填充字幕(假设你知道它是两者中较短的一个,否则你需要在代码中添加额外的逻辑)。

    d <- read.table(text = 
                      "Grp     Var    Col1  Col2    Col3    Sub_grp
    grp_1   8      46.8  50.0   50.6    A
    grp_1   16     95.6  47.4   48.0    A
    grp_1   24     45.1  45.6   46.4    A
    grp_1   32     68.8  44.3   58.2    A
    grp_1   40     44.6  52.2   44.3    A
    grp_1   48     86.5  42.2   68.6    A
    grp_200000   40     63.2  95.6   63.0    B
    grp_200000   60     66.7  67.5   65.6    B
    grp_200000   80     69.6  70.7   67.9    B
    grp_200000   100    71.9  73.4   69.3    B
    grp_200000   120    73.8  75.7   48.0    B
    grp_3   500    51.9  50.0   50.5    C
    grp_3   1000   65.5  53.0   53.4    C
    grp_3   5000   61.2  99.0   59.9    C
    grp_3   10000  80.1  63.0   62.8    C
    grp_3   30000  25.9  33.8   14.2    C
      ", header=T
    )
    
    # Set up a string of spaces corresponding to the length of Grp.
    # Since the strip text is not mono-spaced, I don't use 1/2 the length
    # to center the subtitle - the full length looks about right.
    b <- 1
    G <- as.character(d$Grp)
    for(i in seq_along(G)) b[i]=paste(rep(" ",(nchar(G))[i]),collapse="")
    
    xyplot(
      Col1 + Col2 + Col3 ~ Var | paste(Grp,paste0(b,Sub_grp),sep="\n"),
      data=d,
      superpose=T,
      as.table=TRUE,
      col=c("#cc0000","#008000", "#0073e6"),
      lwd=1.5, ylim = c(0,120),
      ylab = list(label=expression("My Values"), fontsize=15),
      xlab = list(label="Var",fontsize=15),
      key=list(
        text  = list(c("Col1", "Col2", "Col3")),
        lines = list( lwd=1.5, col=c("#cc0000","#008000", "#0073e6")),
        corner = c(1, 0),
        x = 0.90, 
        y = 0.17),
      par.settings = list(strip.background=list(col="white")),
      par.strip.text=list(lines=2),
      scales=list(cex=1.2,x=list(relation="free")),
      type=c("l","g"),layout=c(3,1)
    )
    

    【讨论】:

    • 我尝试了您的建议,将“Grp_1/A”、“Grp_2/B”、“Grp_3/C”放在地块的标题中。我需要两个标题框,就像杂乱的输出中显示的那样...
    • 如果你改为sep="\n",它会将它们放在同一个盒子里的2行上,但盒子可能不够高。如果您以|Sub_grp+Grp 为条件,我认为您将获得所有组合,包括空白组合。
    猜你喜欢
    • 2020-04-14
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2019-08-17
    • 2016-08-04
    相关资源
    最近更新 更多