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

查看:100
本文介绍了如何在格子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)

如果运行代码,您将看到箱须图:由于该论坛上有关grid的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天全站免登陆