如何在格 bwplot 中更改面板标签和 x 轴子标签 [英] How to change panel labels and x-axis sublabels in a lattice bwplot

查看:23
本文介绍了如何在格 bwplot 中更改面板标签和 x 轴子标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm 2 weeks into using RStudio (MacOS) so please forgive me if I'm overlooking an obvious function that would solve my problem.
As a project, I am attempting to reproduce a box plot graph with 4 plots representing Net Benefit, given disease type -- "non-severe"(0) or "severe"(1) -- as an x-axis label, and given treatment -- "talk therapy"(0) or "drug therapy"(1) -- as an x-axis sub-label.

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

Here is my script so far:

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 函数的信息性帖子,我快到了.

If you run the code, you'll see the box-and-whisker plot: I'm almost there thanks to some informative posts on this forum about lattice's bwplot function.

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

However, I am still far from happy with the result. I used the paste0 function to add string descriptors to the X-axis sublabels for treatment group (originally labelled "1,2" and now showing as "Tx 0, Tx 1"), but I would ideally like those sublabels to say "Talk Therapy" and "Drug Therapy", respectively. (I simply didn't know how to do away with the existing numeric labels.)
Similarly, I would like the panel labels to say "Not Severe" where the label is currently 0 and "Severe" where the label is currently 1.

推荐答案

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

The simplest way as far as I can see is to convert your tx and dztype to factors with the appropriate level names.

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)

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

Another solution is to use argument scale for the tx levels and argument strip for 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)))

这篇关于如何在格 bwplot 中更改面板标签和 x 轴子标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆