rplotitalicpropensity-score-matching

R: love.plot() italicize variable name


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)

Solution

  • 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.