R: 使用love.plot()给变量名斜体化。

huangapple go评论73阅读模式
英文:

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(&quot;pacman&quot;)){
install.packages(&#39;pacman&#39;) # install pacman if not already installed;
}
pacman::p_load(tidyverse,
MatchIt, # propensity score matching
cobalt) # covariate balance tables (and plots)
dat &lt;- data.frame(
stringsAsFactors = FALSE,
patients_unique_id = c(&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,
&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;,&quot;10&quot;,&quot;330&quot;,&quot;331&quot;,&quot;332&quot;,
&quot;333&quot;,&quot;334&quot;,&quot;335&quot;,&quot;336&quot;,&quot;337&quot;,&quot;338&quot;,&quot;339&quot;,&quot;340&quot;,
&quot;341&quot;,&quot;342&quot;,&quot;343&quot;,&quot;344&quot;,&quot;11&quot;,&quot;12&quot;,&quot;13&quot;,&quot;14&quot;,&quot;15&quot;),
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 &lt;- matchit(EI_BL ~
ecoli +
kpn +
psa + 
pitt4,
data = dat,
method = &quot;full&quot;,
distance = &quot;glm&quot;)
new_names &lt;- c(ecoli = &quot;**E.coli**&quot;,
kpn = &quot;K.pneumonia&quot;,
psa = &quot;P.aeruginosa&quot;,
pitt4 = &quot;Pitt Bacteremia Score &gt;= 4&quot;)
love.plot(m.out,
drop.distance = TRUE,
var.names = new_names,
abs = TRUE)
sessionInfo()
#&gt; R version 4.2.2 (2022-10-31 ucrt)
#&gt; Platform: x86_64-w64-mingw32/x64 (64-bit)
#&gt; Running under: Windows 10 x64 (build 19044)

答案1

得分: 2

感谢您提供了一个出色的可重现示例并分享了您尝试过的内容。

尝试这个:

p &lt;- love.plot(m.out,
               drop.distance = TRUE,
               abs = TRUE) +
scale_y_discrete(labels = rev(c(expression(italic(&quot;E.coli&quot;)), &quot;K.pneumonia&quot;,
                               &quot;P.aeruginosa&quot;, &quot;Pitt Bacteremia Score &gt;= 4&quot;)))
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 &lt;- love.plot(m.out,
drop.distance = TRUE,
abs = TRUE) +
scale_y_discrete(labels = rev(c(expression(italic(&quot;E.coli&quot;)), &quot;K.pneumonia&quot;,
&quot;P.aeruginosa&quot;, &quot;Pitt Bacteremia Score &gt;= 4&quot;)))
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.

huangapple
  • 本文由 发表于 2023年6月29日 04:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定