1

I am trying to visualize results from MatchIt procedure with bal.plot() from cobalt package.

It works just fine, except I would like to change the lables for the group which by default are "Unadjusted sample" and "Adjusted sample".

bal.plot(AHEAD_nomiss, var.name = "KCH_TKS", which = "both",
         type = "histogram", mirror = F, 
         weights = AHEAD_nomiss$att.weights, treat = AHEAD_nomiss$group)
2
  • 1
    stackoverflow.com/questions/49873861/… , the cobalt package uses ggplot2 as basis for plots.
    – fabla
    Commented Sep 17, 2019 at 8:59
  • I know it does, however the face wrap function is somehow build in function bal.plot and i cant figure out how to change labeller default settings
    – Inga
    Commented Sep 17, 2019 at 10:35

1 Answer 1

0

Author of cobalt package here. Thank you for using my package!

Edit. Original post at the bottom.

I just added some functionality to bal.plot for this in the development version of cobalt, which can be installed with devtools::install_github("ngreifer/cobalt"). Use the sample.names argument to supply a vector of names to give bal.plot and they'll appear in the facet labels. The vector should be as long as the number of samples (in your case, 2). Your new code should look like this:

bal.plot(AHEAD_nomiss, var.name = "KCH_TKS", which = "both",
         type = "histogram", mirror = F, 
         weights = AHEAD_nomiss$att.weights, treat = AHEAD_nomiss$group,
         sample.names = c("UNWEIGHTED", "WEIGHTED"))

Of course you can change the names. If you don't want to install the development version of cobalt (it't not guaranteed to be stable), you can use my solutions below.


I didn't intend bal.plot to be used for publication so I didn't make it super flexible, unlike love.plot. One thing you can do is manually program the histograms using ggplot2. Of course, this requires you learning how to use ggplot2, which can be a challenge, and looking at the source code of bal.plot probably won't help because of all the checks and transformations that occur. Here's some code that might work for you:

unweighted <- data.frame(KCH_TKS = AHEAD_nomiss$KCH_TKS,
                        treat = factor(AHEAD_nomiss$group),
                        weights = 1,
                        adj = "UNWEIGHTED",
                        stringsAsFactors = FALSE)
weighted <- data.frame(KCH_TKS = AHEAD_nomiss$KCH_TKS,
                        treat = factor(AHEAD_nomiss$group),
                        weights = AHEAD_nomiss$att.weights,
                        adj = "WEIGHTED",
                        stringsAsFactors = FALSE)
data <- rbind(unweighted, weighted)

ggplot(data, aes(x = KCH_TKS, fill = treat)) + 
   geom_histogram(aes(weight = weights), bins = 10, alpha = .4, color = "black") + 
   facet_grid(~adj)

One way you can hack bal.plot is to provide a set of weights that are all equal to 1 as well as your desired weights and leave which at its default. If you give the weights names, those names will appear on the facet labels. So, for your example, try

bal.plot(AHEAD_nomiss, var.name = "KCH_TKS", 
         type = "histogram", mirror = F, 
         weights = list(UNWEIGHTED = rep(1, nrow(AHEAD_nomiss),
                        WEIGHTED = AHEAD_nomiss$att.weights), 
         treat = AHEAD_nomiss$group)

You should see that "UNWEIGHTED" and "WEIGHTED" are the new facet label names. You can of course change them to be whatever you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.