# Visualize a continuous variable against a binary variable

I want to visualize a continuous variable, BMI index, against a binary variable, heart disease. I want to check by visualization in my data, if heart disease is more common among higher BMI index cases than lower BMI. I used catplot boxplot:

Plot = sns.catplot(x="heart_disease", y="bmi", kind="box",
data=data);
Plot.fig.suptitle('Correlation between Heart Disease and
BMI index', size=15, y=1.12);


From the result plot, the question to the answer is no. Do you think using boxplot is a good idea? is there another plot type that could do a better job for the question at hand ?

Boxplots lose an enormous amount of information, since they condense all data into just five summary statistics (the median, the box and the whiskers) plus what is unhappily called "outliers".

I would always go with beanplots, also known as violin plots. If you want, you can always overlay boxplots, or the original data. If you do add the original data, jitter them horizontally to avoid overplotting. (And if you plot both boxplots and the original data, suppress the "outliers" plotted by the boxplots, because then you would have the same data plotted twice.) This answer of mine gives an example (the second plot) and R code to create it. In Python, it seems like you could use statsmodels.graphics.boxplots.beanplot or seaborn.violinplot.

Visually checking relationships is a very good idea. However, it is of course subjective. If you want an objective result, consider running a logistic regression of heart_disease on BMI. Consider using splines to capture any nonlinearities.

I usually do overlayed histograms.

• +1. Alternatively, since the OP has only two groups, you could do a back-to-back histogram. Commented Jul 7, 2022 at 9:16

Depending on the sample size, you might do a strip plot (where every observation is a dot in the graph), maybe with jittering or transparent points.

I second Stephen's recommendation of using splines -- the box plot you've shown seems to show that the median and quartiles are roughly similar, but that the outliers are quite different. Oddly, the high outliers all show no heart disease. Was N much larger for that group?

Variants on this question arise frequently: see for example Histogram or box plot, to compare two distributions of means?.

I want to add to the fine answers to date, first by emphasising some small tensions here:

Show the data, but show summaries and expose detail too You should want the data to be seen clearly, but also allow summaries and important detail to jump out at readers. Important detail could be outliers, gaps and spikes as well as skewness and long tails and additive or multiplicative shift between groups. Substantively (meaning, scientifically or practically), people often want to focus on differences in distribution level (means, medians, whatever) between two groups, but differences in distribution spread and shape could complicate or even undermine any comparison based on levels alone.

Familiar method or novel method? There is some advantage to simple methods that are familiar to people in your field, yet there are many small new ideas even on this basic problem that may deserve experiment. Histograms are traditional but often work fine. Box plots for some fields have become traditional too, but their limitations are shown by many small variants aimed to enhance them. Just reference and explain any novel method you use.

Show the data directly? Showing the data directly sounds an obvious goal, but some methods don't do exactly that, in pursuit of clarity or simplicity. Jittering to shake identical points apart and smoothing the density function are contradictory moves, but united by a motive of making overall patterns easier to grasp.

Use a transformed scale? People in different fields can range from those using a transformed scale immediately as a matter of standard measurement convention (pH, Richter scale, decibels) to those suspicious of or unfamiliar with any kind of transformation. Most statistically-minded people are aware of the possibility of showing data on a transformed scale, yet also variable in their enthusiasm for using one in practice.

Here I use a hybrid display with elements of quantile plot (i.e. a plot of all values against a cumulative probability scale), box plot (starting with the idea of median and quartiles in a box), and extra annotation. The goal -- easy to explain, but harder to achieve well -- is to allow readers to see summaries and detail at the same time. Back in 1979, Emanuel Parzen suggested quantile-box plots (reference below). The display below is similar in spirit. Parzen's own examples weren't especially impressive and his readers were perhaps distracted by an attempt in that paper to represent exploratory data analysis in fine mathematical clothes, a project resisted robustly (!) by John W. Tukey in discussion. The name quantile-box plots has also been used since for other related but not identical plots.

The reaction time means (units not stated) from the CV thread cited at the beginning of this answer will serve fine to show some technique. Here is one of many plots that could be shown.

1. These are small samples, 20 values in each, and so it's certainly possible to show each value distinctly without distortion. The design copes reasonably with many more points (200, 2000, 20000, ...) in so far as major details (e.g. marked outliers if they exist) will still jump out.

2. Offsetting point markers according to the associated cumulative probability (rank, equivalently) avoids or at least reduces any call for jittering. If the underlying distribution is fairly smooth, so too will be the quantile trace.

3. The cumulative probability scale here is linear, but other scales could make as much or more sense (e.g. transformed to unit normal deviates).

4. Parzen superimposed quantile and box plots (that was much of his point, that quantile and box plots share links to cumulative probabilities) and I do that too sometimes. Here they are juxtaposed.

5. Box plots are presented but here have only a summary role (and so can be made quite thin without loss). As the quantile plot shows all the data, we needn't concern ourselves with any rule or convention about which data points are shown individually. The most common convention used seems to remain the convention that Tukey settled on after some experiment, namely to show all points individually that lie more than 1.5 IQR from the nearer quartile. I find in my reading and discussion that (a) teachers and researchers are often poor in explaining the convention that they used, and readers are often less familiar with such conventions than is assumed; (b) there is even a tendency to take that rule of thumb for which data points are to be shown individually as a hard-and-fast criterion for outliers, which is unfortunate. It's partly a matter of taste, but I often like to revert to a practice of just taking the whiskers out to paired percentiles in the tails, which is quite often done in various literatures. Which percentiles is not, and need not be, standardized and that should not matter much so long as a choice is explained.

6. I add longer lines showing the means. Naturally, that could be done too with distinct point symbols. Show geometric means, midmeans or anything else instead if that fits the problem or the data better. I note a bizarre but common habit of accompanying discussions of t-tests or analysis of variance with box plots that don't show means too; that is much better than no graph at all, but is like Romeo and Juliet without Romeo and Juliet.

7. As above, this display is compatible with transformations, the major detail to be clear whether log(mean) or mean(log) is being shown. On a logarithmic scale showing geometric means to compare with medians is a really good idea.

As a matter of record, I show Stata code I used. If your favourite software doesn't make something like this fairly easy, you need a new favourite.

clear
input float(A B)
.8792397 .9964306
.5845183 .7269523
.8092829 .9343457
.6869933 .9014275
.7223416 .7856004
.6551149 .8224649
.6549308 .8670868
.6560364 .7797318
.602458 .8236209
.6110293 .8315545
.6373121  .774942
.6295298 .8020451
.6144096 .7622244
.6776401 .8087511
.6165493 .7977815
.6055175 .7647625
.6318304 .7652289
.6315798 .7064912
.6329535 .7116355
.5817685 .7861449
end

stripplot A B, cumul vertical box(barw(0.1))  refline pctile(5) boffset(-0.1) ///
height(0.4) yla(, ang(h)) ytitle("") ///
note("box and whiskers show median, quartiles, 5% and 95% percentiles" "longer lines show means") ///
xla(, tlc(bg)) ytitle("more explanation goes here")


Parzen, E. 1979. Nonparametric statistical data modeling. Journal of the American Statistical Association 74(365): 105–121. https://doi.org/10.2307/2286734