英文:
R: love.plot() italicize variable name
问题
我创建了一个爱心图来展示倾向得分匹配结果的协变量平衡。我想将细菌的名称如 E.coli 斜体显示,但似乎 **
不起作用。我还尝试了 expression(italic())
,但没有成功。有什么建议吗?
if(!require("pacman")){
install.packages('pacman') # 如果尚未安装,请安装 pacman;
}
pacman::p_load(tidyverse,
MatchIt, # 倾向得分匹配
cobalt) # 协变量平衡表格(和图表)
dat <- data.frame(
stringsAsFactors = FALSE,
patients_unique_id = c("1","2","3","4",
"5","6","7","8","9","10","330","331","332",
"333","334","335","336","337","338","339","340",
"341","342","343","344","11","12","13","14","15"),
EI_BL = c(0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,1,0,1),
ecoli = c(0,0,0,0,0,1,0,0,1,0,
1,1,0,0,0,1,0,1,1,1,0,0,
1,0,0,0,0,0,0,1),
kpn = c(0,0,0,0,0,0,1,0,0,0,
0,0,1,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0),
psa = c(0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0),
pitt4 = c(0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,1,0,0,0,0,
0,0,1,0,0,0,0,0)
)
m.out <- matchit(EI_BL ~
ecoli +
kpn +
psa +
pitt4,
data = dat,
method = "full",
distance = "glm")
new_names <- c(ecoli = "**E.coli**",
kpn = "K.pneumonia",
psa = "P.aeruginosa",
pitt4 = "Pitt Bacteremia Score >= 4")
love.plot(m.out,
drop.distance = TRUE,
var.names = new_names,
abs = TRUE)
sessionInfo()
#> R version 4.2.2 (2022-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19044)
英文:
I created a love plot to show covariate balance of the propensity score matched results. I'd like to italicize names of bacteria e.g. E.coli instead E.coli, but **
doesn't seem to work. I also tried expression(italic())
to no avail.
Any ideas?
if(!require("pacman")){
install.packages('pacman') # install pacman if not already installed;
}
pacman::p_load(tidyverse,
MatchIt, # propensity score matching
cobalt) # covariate balance tables (and plots)
dat <- data.frame(
stringsAsFactors = FALSE,
patients_unique_id = c("1","2","3","4",
"5","6","7","8","9","10","330","331","332",
"333","334","335","336","337","338","339","340",
"341","342","343","344","11","12","13","14","15"),
EI_BL = c(0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,1,0,1),
ecoli = c(0,0,0,0,0,1,0,0,1,0,
1,1,0,0,0,1,0,1,1,1,0,0,
1,0,0,0,0,0,0,1),
kpn = c(0,0,0,0,0,0,1,0,0,0,
0,0,1,0,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0),
psa = c(0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0),
pitt4 = c(0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,1,0,0,0,0,
0,0,1,0,0,0,0,0)
)
m.out <- matchit(EI_BL ~
ecoli +
kpn +
psa +
pitt4,
data = dat,
method = "full",
distance = "glm")
new_names <- c(ecoli = "**E.coli**",
kpn = "K.pneumonia",
psa = "P.aeruginosa",
pitt4 = "Pitt Bacteremia Score >= 4")
love.plot(m.out,
drop.distance = TRUE,
var.names = new_names,
abs = TRUE)
sessionInfo()
#> R version 4.2.2 (2022-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19044)
答案1
得分: 2
感谢您提供了一个出色的可重现示例并分享了您尝试过的内容。
尝试这个:
p <- love.plot(m.out,
drop.distance = TRUE,
abs = TRUE) +
scale_y_discrete(labels = rev(c(expression(italic("E.coli")), "K.pneumonia",
"P.aeruginosa", "Pitt Bacteremia Score >= 4")))
p
您可以在love.plot
之后添加ggplot2
的内容。我只是将它保存为一个对象p
。当您在命令行中调用p
时,它会绘制。y轴中的水平线是从底部到顶部绘制的,这就是为什么我需要使用rev()
的原因。
另外,参考这个示例1。
英文:
Thank you for giving us an excellent reproducible example and sharing what you tried.
Try this:
p <- love.plot(m.out,
drop.distance = TRUE,
abs = TRUE) +
scale_y_discrete(labels = rev(c(expression(italic("E.coli")), "K.pneumonia",
"P.aeruginosa", "Pitt Bacteremia Score >= 4")))
p
You can add ggplot2
stuff after love.plot
. I just saved it as an object p
here. When you call p
from the command line, it will plot. The levels in the y axis are done from bottom to top, and so that's why I needed a rev()
.
Also, see this example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论