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

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

R: love.plot() italicize variable name

问题

我创建了一个爱心图来展示倾向得分匹配结果的协变量平衡。我想将细菌的名称如 E.coli 斜体显示,但似乎 ** 不起作用。我还尝试了 expression(italic()),但没有成功。有什么建议吗?

  1. if(!require("pacman")){
  2. install.packages('pacman') # 如果尚未安装,请安装 pacman;
  3. }
  4. pacman::p_load(tidyverse,
  5. MatchIt, # 倾向得分匹配
  6. cobalt) # 协变量平衡表格(和图表)
  7. dat <- data.frame(
  8. stringsAsFactors = FALSE,
  9. patients_unique_id = c("1","2","3","4",
  10. "5","6","7","8","9","10","330","331","332",
  11. "333","334","335","336","337","338","339","340",
  12. "341","342","343","344","11","12","13","14","15"),
  13. EI_BL = c(0,0,0,0,0,0,0,0,0,0,
  14. 1,0,0,0,0,0,0,0,0,0,0,0,
  15. 0,1,0,0,0,1,0,1),
  16. ecoli = c(0,0,0,0,0,1,0,0,1,0,
  17. 1,1,0,0,0,1,0,1,1,1,0,0,
  18. 1,0,0,0,0,0,0,1),
  19. kpn = c(0,0,0,0,0,0,1,0,0,0,
  20. 0,0,1,0,0,0,0,0,0,0,0,0,
  21. 0,0,1,0,0,0,0,0),
  22. psa = c(0,0,0,0,0,0,0,1,0,0,
  23. 0,0,0,0,0,0,0,0,0,0,0,0,
  24. 0,0,0,1,0,0,0,0),
  25. pitt4 = c(0,0,0,0,0,0,0,1,0,0,
  26. 0,0,0,0,0,0,0,1,0,0,0,0,
  27. 0,0,1,0,0,0,0,0)
  28. )
  29. m.out <- matchit(EI_BL ~
  30. ecoli +
  31. kpn +
  32. psa +
  33. pitt4,
  34. data = dat,
  35. method = "full",
  36. distance = "glm")
  37. new_names <- c(ecoli = "**E.coli**",
  38. kpn = "K.pneumonia",
  39. psa = "P.aeruginosa",
  40. pitt4 = "Pitt Bacteremia Score >= 4")
  41. love.plot(m.out,
  42. drop.distance = TRUE,
  43. var.names = new_names,
  44. abs = TRUE)
  1. sessionInfo()
  2. #> R version 4.2.2 (2022-10-31 ucrt)
  3. #> Platform: x86_64-w64-mingw32/x64 (64-bit)
  4. #> 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?

  1. if(!require(&quot;pacman&quot;)){
  2. install.packages(&#39;pacman&#39;) # install pacman if not already installed;
  3. }
  4. pacman::p_load(tidyverse,
  5. MatchIt, # propensity score matching
  6. cobalt) # covariate balance tables (and plots)
  7. dat &lt;- data.frame(
  8. stringsAsFactors = FALSE,
  9. patients_unique_id = c(&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,
  10. &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;,
  11. &quot;333&quot;,&quot;334&quot;,&quot;335&quot;,&quot;336&quot;,&quot;337&quot;,&quot;338&quot;,&quot;339&quot;,&quot;340&quot;,
  12. &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;),
  13. EI_BL = c(0,0,0,0,0,0,0,0,0,0,
  14. 1,0,0,0,0,0,0,0,0,0,0,0,
  15. 0,1,0,0,0,1,0,1),
  16. ecoli = c(0,0,0,0,0,1,0,0,1,0,
  17. 1,1,0,0,0,1,0,1,1,1,0,0,
  18. 1,0,0,0,0,0,0,1),
  19. kpn = c(0,0,0,0,0,0,1,0,0,0,
  20. 0,0,1,0,0,0,0,0,0,0,0,0,
  21. 0,0,1,0,0,0,0,0),
  22. psa = c(0,0,0,0,0,0,0,1,0,0,
  23. 0,0,0,0,0,0,0,0,0,0,0,0,
  24. 0,0,0,1,0,0,0,0),
  25. pitt4 = c(0,0,0,0,0,0,0,1,0,0,
  26. 0,0,0,0,0,0,0,1,0,0,0,0,
  27. 0,0,1,0,0,0,0,0)
  28. )
  29. m.out &lt;- matchit(EI_BL ~
  30. ecoli +
  31. kpn +
  32. psa +
  33. pitt4,
  34. data = dat,
  35. method = &quot;full&quot;,
  36. distance = &quot;glm&quot;)
  37. new_names &lt;- c(ecoli = &quot;**E.coli**&quot;,
  38. kpn = &quot;K.pneumonia&quot;,
  39. psa = &quot;P.aeruginosa&quot;,
  40. pitt4 = &quot;Pitt Bacteremia Score &gt;= 4&quot;)
  41. love.plot(m.out,
  42. drop.distance = TRUE,
  43. var.names = new_names,
  44. abs = TRUE)
  1. sessionInfo()
  2. #&gt; R version 4.2.2 (2022-10-31 ucrt)
  3. #&gt; Platform: x86_64-w64-mingw32/x64 (64-bit)
  4. #&gt; Running under: Windows 10 x64 (build 19044)

答案1

得分: 2

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

尝试这个:

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

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

确定