【问题标题】:How to change panel labels and x-axis sublabels in a lattice bwplot如何在格 bwplot 中更改面板标签和 x 轴子标签
【发布时间】:2013-02-05 08:31:07
【问题描述】:

我使用 RStudio (MacOS) 已经 2 周了,所以如果我忽略了一个可以解决我的问题的明显功能,请原谅我。
作为一个项目,我试图重现一个箱线图,其中 4 个图代表净收益,给定疾病类型——“非严重”(0)或“严重”(1)——作为 x 轴标签,以及给予治疗——“谈话疗法”(0) 或“药物疗法”(1)——作为 x 轴子标签。

到目前为止,这是我的脚本:

tx <- c(0,0,0,0,1,1,1,1)
dztype <- c(1,0,1,0,0,0,1,1)
NBwtp1000 <- c(-5500,-4000,-5000,-1000,-5000,-5000,-2800,-2000)
require(lattice)
bwplot(NBwtp1000 ~ paste0("Tx ", tx) | paste0("Disease Severity ", dztype),
       xlab="Talk Therapy (Tx 0) or Drug Therapy (Tx 1)", 
       ylab="Net Benefit @ wtp 1000", horizontal=FALSE)

如果您运行代码,您将看到盒须图:感谢本论坛上有关 lattice 的 bwplot 函数的一些信息性帖子,我快到了。

但是,我对结果仍然很不满意。我使用paste0 函数将字符串描述符添加到治疗组的 X 轴子标签(最初标记为“1,2”,现在显示为“Tx 0,Tx 1”),但理想情况下,我希望这些子标签说分别是“谈话疗法”和“药物疗法”。 (我根本不知道如何取消现有的数字标签。)
同样,我希望面板标签在标签当前为 0 时显示“不严重”,在标签当前为 1 时显示“严重”。

【问题讨论】:

    标签: r labels lattice boxplot


    【解决方案1】:

    据我所知,最简单的方法是将您的 txdztype 转换为具有适当级别名称的因子。

    tx <- factor(tx, levels=c(0,1), labels=c("Talk Therapy", "Drug Therapy"))
    dztype <- factor(dztype, levels=c(0,1), labels=c("Not severe", "Severe"))
    bwplot(NBwtp1000 ~ tx | dztype, xlab="Talk Therapy or Drug Therapy", 
           ylab="Net Benefit @ wtp 1000", horizontal=FALSE)
    

    另一种解决方案是使用参数scale 表示tx 级别,使用参数strip 表示dztype

    tx <- c(0,0,0,0,1,1,1,1)
    dztype <- c(1,0,1,0,0,0,1,1)
    bwplot(NBwtp1000 ~ tx | dztype, xlab="Talk Therapy or Drug Therapy", 
           ylab="Net Benefit @ wtp 1000", horizontal=FALSE,
           scales=list(x=list(labels=c("Talk therapy","Drug Therapy"))), 
           strip=strip.custom(var.name="Disease severity", 
                              factor.levels=c(" Not Severe", "Severe"),
                              strip.levels=rep(TRUE,2)))
    

    【讨论】:

      猜你喜欢
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 2021-04-08
      • 2018-02-06
      • 1970-01-01
      • 2015-02-10
      • 2018-01-10
      相关资源
      最近更新 更多