我使用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)
如果你运行代码,你会看到盒子和胡须的情节:我几乎要感谢这个论坛上有关格子bwplot
功能的一些信息性帖子。
但是,我对结果仍然很满意。我使用paste0
函数将字符串描述符添加到治疗组的X轴子标签(最初标记为“1,2”,现在显示为“Tx 0,Tx 1”),但我理想地喜欢那些子标签分别说“谈话疗法”和“药物疗法”。 (我根本不知道如何取消现有的数字标签。)
同样,我希望面板标签在标签当前为0时显示“Not Severe”,在标签当前为1时为“Severe”。
答案 0 :(得分:8)
据我所知,最简单的方法是将您的tx
和dztype
转换为具有相应级别名称的因素。
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)))