# What is the best way to visualize relationship between discrete and continuous variables?

What is the best way to show a relationship between:

• continuous and discrete variable,
• two discrete variables ?

So far I have used scatter plots to look at the relationship between continuous variables. However in case of discrete variables data points are cumulated at certain intervals. Thus the line of best fit might be biased.

• For the discrete-discrete case, this answer to a somewhat related question here, on plotting ordered categorical data may help (though possibly without the boxes in your case). I'm really not sure how you think this 'bias' arises; it would affect the visual impression of the data points (leading to use expecting the line to go somewhere other than where it should) but not the actual data itself. Can you explain your reasoning here? – Glen_b Jun 5 '13 at 0:29

Below: The original plot may be misleading because the discrete nature of the variables makes the points overlap:

One way to work around it is to introduce some transparency to the data symbol:

Another way is to displace the location of the symbol mildly to create a smear. This technique is called "jittering:"

Both solutions will still allow you to fit a straight line to assess linearity.

x <- trunc(runif(200)*10)
y <- x * 2 + trunc(runif(200)*10)
plot(x,y,pch=16)
plot(x,y,col="#00000020",pch=16)
plot(jitter(x),jitter(y),col="#000000",pch=16)

• Nice answer. What about a bubble-scatter plots with variable instance counts? I tried using these techniques on a massive data set and it all took too long rendering the alphas. – josh Jul 4 '16 at 9:43

I would use boxplots to display the relationship between a discrete and a continuous variable. You can make your boxplots vertical or horizontal with standard statistical software, so it's easy to visualize as either IV or DV. It is possible to use a scatterplot with a discrete and continuous variable, just assign a number to the discrete variable (e.g., 1 & 2), and jitter those values (note top plot on right here).

Regarding your comment that the line of best fit might be biased, it depends on what you have. For instance, if you have a discrete variable with two levels as your IV, and a continuous variable as your DV, you can draw a line through the two means and this will not be biased. (We would typically think of this situation as being appropriate for a t-test, but it is actually a form--i.e., simple case--of regression, see my answer here.) On the other hand, if you have a discrete variable with two levels as your DV, standard (OLS) regression would be inappropriate (logistic regression would be called for) and the line of best fit would be biased, but you could fit (& plot) a lowess line as part of your initial data exploration.

For visualizing the relationship between two discrete variables, I would use a mosaic plot. You could also use a sieve plot, an association plot, or a dynamic pressure plot with some programming.

When considering the relationship between a binary outcome variable and a continuous predictor, I would use the loess smoother (with outlier detection turned off, e.g., in R lowess(x, y, iter=0).

In the next release of the R Hmisc package you can easily create a single lattice graphic that puts such curves into a multipanel display for multiple predictors, e.g.

summaryRc(heart.attack ~ age + blood.pressure + weight, data=mydata)


If you are not satisfied with simple scatter plots you might want to add the frequencies of the data points at each value of the discrete variable. How to do this then just depends on the statistical program you are using. Here is an example for Stata. You can also apply this to the scatter plot of two categorical variables. Otherwise a box plot or overlaid bar charts may be fine but this really depends on how you want to present these variables.

I found a paper applicable on association between two binary variables on http://www.boekboek.com/xb130929113026 - here, in that article it is shown and proved that the strenght of association between two binary variables can be expressed as a fraction of perfect association. So it becomes possible and preferable to state: the association between variable A and variable B is for instance 50% instead of the contemporarily stating: OR = 9 (not easy to interpret) or the realtive risk = 2 (contemporarily the relative risk is considered too to be a measure of association although in fact it is a function of association, prevalence or incidence and positivity).