2.4 Bivariate Visualizations

2.4.1 Categorical v. Categorical

2.4.1.1 Two-way Frequency tables

Cross-tabs, cross-tabulations and two-way tables (all the same thing, different names) can be created by using the table() function.

The frequency table is constructed using the table() function.

There are 4 Fair diamonds with color D, and 21 Ideal quality diamonds with color J.

2.4.1.2 Two-way Proprtion tables

Choose your percentages depending on your research question. What are you wanting to compare?

Best practices:

  • Explanatory variable on the rows
  • Response variable on the columns
  • Calculate row %’s as the % of the response for each explanatory group.

Here are demonstrations of how the interpretation of the percents change depending on what the denominator is.

Cell proportions

Wrapping prop.table() around a table gives you the cell proportions.

0.4% of all diamonds are D color and Fair cut, 2.1% are J color and Ideal cut.

Row proportions

To get the row proportions, you specify margin=1. The percentages now add up to 1 across the rows.

14.8% of all Fair quality diamonds are color D. 5.4% of all Ideal quality diamonds have color J.

Column proportions

To get the column proportions, you specify margin=2. The percentages now add up to 1 down the columns.

2.7% of all D color diamonds are of Fair quality. 44.7% of all J color diamonds are of Ideal quality.

2.4.1.3 Grouped bar charts

To compare proportions of one categorical variable within the same level of another, is to use grouped barcharts.

base As before, the object to be plotted needs to be the result of a table.

Stacked bars can be difficult to interpret, and very difficult to compare values between groups. A side by side barchart is preferable. The beside=TRUE is what controls the placement of the bars.

ggplot Again plot the cut on the x axis, but then fill using the second categorical variable. This has the effect of visualizing the row percents from the table above. The percent of color, within each type of cut.

Again the default is a stacked barchart. So we just specify position=dodge to put the bars side by side.

And look, an automatic legend. What if I wanted to better compare cut within color group? This is the column percentages. Just switch which variable is the x axis and which one is used to fill the colors!

For more than 2 colors I do not recommend choosing the colors yourself. I know little about color theory so I use the built-in color palettes. Here is a great cheatsheet about using color palettes.

And this easy change is why we love ggplot2.

2.4.2 Grouped bar charts with percentages

Not as easy as one would hope, but the solution is to calculate the desired percentages first and then plot the summary data using either geom_bar(stat='identity') or geom_col().

Since we’re plotting summary data, the height of the bars is specified using y=pct.

Now set some options to the y axis using scale_y_continuous() to make the graph more accurate and readable. The labels=percent comes from the scales package.

2.4.2.1 sjPlot

sjPlot does a very nice job of being able to cleanly show not only n’s but percents.

2.4.2.2 Mosaic plots

But what if you want to know how two categorical variables are related and you don’t want to look at two different barplots? Mosaic plots are a way to visualize the proportions in a table. So here’s the two-way table we’ll be plotting.

The syntax for a mosaic plot uses model notation, which is basically y ~ x where the ~ is read as “twiddle” or “tilde”. It’s to the left of your 1 key.

Helpful, ish. Here are two very useful options. In reverse obviousness, color applies shades of gray to one of the factor levels, and shade applies a color gradient scale to the cells in order of what is less than expected (red) to what is more than expected (blue) if these two factors were completely independent.

For example, there are fewer ‘Very Good’ cut diamonds that are color ‘G’, and fewer ‘Premium’ cut diamonds that are color ‘H’. As you can see, knowing what your data means when trying to interpret what the plots are telling you is essential.

That’s about all the ways you can plot categorical variables. If you are wondering why there was no 3D barcharts demonstrated see
here, here, and here for other ways you can really screw up your visualization.

2.4.3 Continuous v. Continuous

2.4.3.1 Scatterplot

The most common method of visualizing the relationship between two continuous variables is by using a scatterplot.

base Back to the plot() command. Here we use model notation again, so it’s \(y~x\).

Looks like for the most part as the carat value increases so does price. That makes sense.

ggplot With ggplot we specify both the x and y variables, and add a point.

Other Resources

2.4.3.2 Adding lines to the scatterplots

Two most common trend lines added to a scatterplots are the “best fit” straight line and the “lowess” smoother line.

base The best fit line (in blue) gets added by using the abline() function wrapped around the linear model function lm(). Note it uses the same model notation syntax and the data= statement as the plot() function does. The lowess line is added using the lines() function, but the lowess() function itself doesn’t allow for the data= statement so we have to use $ sign notation.

ggplot With ggplot, we just add a geom_smooth() layer.

Here the point-wise confidence interval for this lowess line is shown in grey. If you want to turn the confidence interval off, use se=FALSE. Also notice that the smoothing geom uses a different function or window than the lowess function used in base graphics.

Here it is again using the ggplot plotting function and adding another geom_smooth() layer for the lm (linear model) line in blue, and the lowess line (by not specifying a method) in red.

2.4.3.3 Line plots

Line plots connect each dot with a straight line. This is most often done when measuring trends of the response as the value of x increases (such as a time series)

We saw earlier that carat and price seemed possibly linear. Let see how the average price changes with carat.

base For base graphics, type=‘b’ means both points and lines, ‘l’ gives you just lines and ‘p’ gives you only points. You can find more plotting character options under ?pch.

ggplot With ggplot we specify that we want a line geometry only.

How does this relationship change with cut of the diamond? First lets get the average price per combination of carat and cut.

base This plot can be created in base graphics, but it takes an advanced knowledge of the graphics system to do so. So I do not show it here.

ggplot This is where ggplot starts to excel in it’s ease of creating more complex plots. All we have to do is specify that we want the lines colored by the cut variable.

And we get one line per cut.

2.4.4 Continuous v. Categorical

Create an appropriate plot for a continuous variable, and plot it for each level of the categorical variable.

2.4.4.1 Dotplot/strip chart

Dotplots can be very useful when plotting dots against several categories. They can also be called stripcharts.

base

Doesn’t look to pretty, but kinda gets the point across. Few fair quality diamonds in the data set, pretty spread out across the carat range except one high end outlier.

ggplot We can reproduce the same thing by plotting one continuous variable against one categorical variable, and adding a layer of points. I’d argue that horizontal looks better due to the axis-labels.

2.4.4.2 Grouped boxplots

base Base graphics plots grouped boxplots with also just the addition of a twiddle (tilde) ~. Another example of where model notation works.

ggplot A simple addition, just define your x and y accordingly.

Adding violins Violin plots can be overlaid here as well.

2.4.4.3 Grouped histograms

base There is no easy way to create grouped histograms in base graphics we will skip it.

ggplot By default ggplot wants to overlay all plots on the same grid. This doesn’t look to good with histograms. Instead you can overlay density plots

The solid fills are still difficult to read, so we can either turn down the alpha (turn up the transparency) or only color the lines and not the fill.

2.4.5 Joy plots / Ridgelines

Somewhat new (2017), joylines have not been added to the base distribution of ggplot2 yet. For now it’s available in the ggjoy package. Really good way to visualize density plots without the overlapping issue.