我使用RStudio(MacOS)已有2周的时间,因此如果我忽略了可以解决我的问题的明显功能,请原谅我。
作为一个项目,我尝试复制一个箱形图,其中包含4个代表净 yield 的图,给定疾病类型-“非严重”(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”),但是理想情况下,我希望这些子标签说“Talk疗法”和“药物疗法”。 (我根本不知道如何删除现有的数字标签。)同样,我希望面板标签在标签当前为0的情况下说“严重”,在标签当前为1的情况下说“严重”。
最佳答案
据我所知,最简单的方法是将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)))
关于r - 如何在格子bwplot中更改面板标签和x轴子标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14703094/