| love.plot | R Documentation |
Generates a "Love" plot graphically displaying covariate balance before and after adjusting. Options are available for producing publication-ready plots. Detailed examples are available in vignette("love.plot").
love.plot(
x,
stats,
abs,
agg.fun = NULL,
var.order = NULL,
drop.missing = TRUE,
drop.distance = FALSE,
thresholds = NULL,
line = FALSE,
stars = "none",
grid = FALSE,
limits = NULL,
colors = NULL,
shapes = NULL,
alpha = 1,
size = 3,
wrap = 30,
var.names = NULL,
title,
sample.names,
labels = FALSE,
position = "right",
themes = NULL,
...
)
x |
the valid input to a call to |
stats |
|
abs |
|
agg.fun |
if balance is to be displayed across clusters or imputations rather than within a single cluster or imputation, which summarizing function ("mean", "max", or "range") of the balance statistics should be used. If "range" is entered, |
var.order |
a |
drop.missing |
|
drop.distance |
|
thresholds |
|
line |
|
stars |
when mean differences are to be displayed, which variable names should have a star (i.e., an asterisk) next to them. Allowable values are |
grid |
|
limits |
|
colors |
the colors of the points on the plot. See 'Color Specification' at |
shapes |
the shapes of the points on the plot. Must be one or two numbers between 1 and 25 or the name of a valid shape. See the ggplot2 aesthetic specifications vignette ( |
alpha |
|
size |
|
wrap |
|
var.names |
an optional object providing alternate names for the variables in the plot, which will otherwise be the variable names as they are stored. This may be useful when variables have ugly names. See |
title |
|
sample.names |
|
labels |
|
position |
the position of the legend. When |
themes |
an optional list of |
... |
additional arguments passed to
Additionally, any of the |
love.plot() can be used with clusters, imputations, and multi-category and longitudinal treatments in addition to the standard case. Setting the corresponding which. argument to .none will aggregate across that dimension. When aggregating, an argument should be specified to agg.fun referring to whether the mean, minimum ("min"), or maximum ("max") balance statistic or range ("range", the default) of balance statistics for each covariate should be presented in the plot. See vignette("segmented-data") for examples.
With subclasses, balance will be displayed for the unadjusted sample and the aggregated subclassified sample. If disp.subclass is TRUE, each subclass will be displayed additionally as a number on the plot.
var.orderThe order that the variables are presented in depends on the argument to var.order. If NULL, the default, they will be displayed in the same order as in the call to bal.tab(), which is the order of the underlying data set. If "alphabetical", they will be displayed in alphabetical order. If "unadjusted", they will be ordered by the balance statistic of the unadjusted sample. To order by the values of the adjusted sample, "adjusted" can be supplied if only one set of weights (or subclasses) are specified; otherwise, the name of the set of weights should be specified.
If multiple stats are requested, the order will be determined by the first entry to stats; for example, if both "mean.diffs" and "ks.statistics" are requested and var.order = "unadjusted", the variables will be displayed in order of the unadjusted mean differences for both plots. If multiple plots are produced simultaneously (i.e., for individual clusters or imputations), var.order can only be NULL or "alphabetical".
If a love.plot object is supplied, the plot being drawn will use the variable order in the supplied love.plot object. This can be useful when making more than one plot and the variable order should be the same across plots.
var.namesThe variables are presented as they are named in the output of the call to bal.tab(), which may not be how they should be named in a report. var.names supplies alternate names for them; it is documented at display-options, along with the several structures it accepts and how a name given for a base variable reaches the factor levels, polynomials, and interactions it appears in. It is one of the display options, so it can be given to bal.tab() instead, in which case the plot uses those names without being told them again.
The factor_sep and int_sep display options are taken here too, so the separators a plot displays can be chosen without recomputing the object; see display-options.
When mean differences are to be displayed, love.plot() attempts to figure out the appropriate label for the x-axis. If all mean differences are standardized, the x-axis label will be "Standardized Mean Differences". If all mean differences are raw (i.e., unstandardized), the x-axis label will be "Mean Differences". Otherwise, love.plot() turns to the stars argument. If "raw", the x-axis label will be "Standardized Mean Differences" (i.e., because un-starred variables have standardized mean differences displayed). If "std", the x-axis label will be "Mean Differences" (i.e., because un-starred variables have raw mean differences displayed). If "none", the x-axis label will be "Mean Differences" and a warning will be issued recommending the use of stars.
The default is to display standardized mean differences for continuous variables, raw mean differences for binary variables, and no stars, so this warning will be issued in most default uses of love.plot(). The purpose of this is to correct behavior of previous versions of cobalt in which the default x-axis label was "Mean Differences", even when standardized mean differences were displayed, yielding a potentially misleading plot. This warning requires the user to think about what values are being displayed. The idea of using stars is that the user can, in a caption for the plot, explain that variables with an asterisk have standardized (or raw) mean differences display, in contrast to un-starred variables.
When only one type of balance statistic is requested, the returned object is a standard ggplot object that can be manipulated using ggplot2 syntax. This facilitates changing fonts, background colors, and features of the legend outside of what love.plot() provides automatically.
When more than one type of balance statistic is requested, the plot is constructed using gridExtra::arrangeGrob(), which arranges multiple plots and their shared legend into one plot. Because the output of arrangeGrob() is a gtable object, its features cannot be manipulated in the standard way. Use the themes argument to change theme elements of the component plots. The original plots are stored in the "plots" attribute of the output object.
love.plot can also be called by using plot() or autoplot() on a bal.tab object. If used in this way, some messages may appear twice. It is recommended that you just use love.plot() instead.
bal.tab(), vignette("love.plot")
display-options for var.names
data("lalonde", package = "cobalt")
## Propensity score weighting
library(WeightIt)
w.out1 <- weightit(treat ~ age + educ + race + married +
nodegree + re74 + re75,
data = lalonde)
love.plot(w.out1, thresholds = c(m = .1),
var.order = "unadjusted")
## Using alternate variable names
v <- data.frame(old = c("age", "educ", "race_black", "race_hispan",
"race_white", "married", "nodegree", "re74",
"re75", "distance"),
new = c("Age", "Years of Education", "Black",
"Hispanic", "White", "Married", "No Degree",
"Earnings 1974", "Earnings 1975",
"Propensity Score"))
love.plot(w.out1, stats = "m", threshold = .1,
var.order = "unadjusted", var.names = v)
#Using multiple stats
love.plot(w.out1, stats = c("m", "ks"),
thresholds = c(m = .1, ks = .05),
var.order = "unadjusted", var.names = v, stars = "raw",
position = "bottom", wrap = 20)
#Changing visual elements
love.plot(w.out1, thresholds = c(m = .1),
var.order = "unadjusted", var.names = v, abs = TRUE,
shapes = c("triangle filled", "circle"),
colors = c("red", "blue"), line = TRUE,
grid = FALSE, sample.names = c("Original", "Weighted"),
stars = "raw", position = "top")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.