I use R code in the backend of my application to generate different plots such as histograms after conducting matching. In order to show the plots in the front end of my application I want to extract all data values and rebuild them using JavaScript/React. However, I fail to read out all data values when using mirrored histograms.
Here is a reproducable example of my backend code:
library(MatchIt)
library(cobalt)
# example data from cobalt
data("lalonde", package = "cobalt")
# conducting matching with MatchIt
m.out <- matchit(treat ~ age + educ + race +
married + nodegree + re74 + re75,
data = lalonde)
# creating histogram from matchit object with cobalt
bal.plot(m.out, "educ", which = "both", mirror = TRUE,
type = "histogram", colors = c("#1C4189", "#B11B18"))
The code generates the histogram shown in the image: enter image description here
I use the following approach to read out data from the generated histogram:
library("ggplot2")
# read out data from histogram by converting it to a ggplot object
histo <- bal.plot(m.out, "educ", which = "both", mirror = TRUE,
type = "histogram", colors = c("#1C4189", "#B11B18"))
histo_gplot <- ggplot_build(histo)
print(histo_gplot$data[[1]]$y)
However all I get is values for treatment = 0 (blue bars):
0.006993007 0.009324009 0.023310023 0.020979021 0.083916084 0.202797203 0.130536131 0.393939394 0.044289044 0.058275058 0.020979021 0.004662005 0.000000000 0.010810811 0.016216216 0.016216216 0.043243243 0.205405405 0.178378378 0.372972973 0.048648649 0.064864865 0.032432432 0.010810811
The first 12 values seem to represent the unadjusted sample histogram (upper-left), while the last 12 data values refer to the adjusted sample (upper-right histogram).
Now I have no idea how to read out the values for the remaining two histograms on the bottom (red bars). histo_gplot$data[[2]]$y
and histo_gplot$data[[3]]$y
do not give any results.
Does anyone have an idea, how I can get the values of the remaining histograms the same way I received the values for the upper histograms?