Introduction to Data Science - 9  Data visualization principles (2024)

In this chapter we aim to provide some general principles we can use as a guide for effective data visualization. Much of this section is based on a talk by Karl Broman1 titled “Creating Effective Figures and Tables”2 and includes some of the figures which were made with code that Karl makes available on his GitHub repository3, as well as class notes from Peter Aldhous’ Introduction to Data Visualization course4. Following Karl’s approach, we show some examples of plot styles we should avoid, explain how to improve them, and use these as motivation for a list of principles. We compare and contrast plots that follow these principles to those that don’t.

The principles are mostly based on research related to how humans detect patterns and make visual comparisons. The preferred approaches are those that best fit the way our brains process visual information. When deciding on a visualization approach, it is also important to keep our goal in mind. We may be comparing a viewable number of quantities, describing distributions for categories or numeric values, comparing the data from two groups, or describing the relationship between two variables. As a final note, we want to emphasize that it is important to adapt and optimize graphs to the audience. For example, an exploratory plot made for ourselves will be different than a chart intended to communicate a finding to a general audience.

In this chapter we focus on the principles and do not show code (code can be viewed on GitHub5). In Chapter10, we apply these principles in case study and do show code.

9.1 Encoding data using visual cues

We start by describing some principles for encoding data. There are several visual cues at our disposal including position, aligned lengths, angles, area, brightness, and color hue.

To illustrate how some of these visual cues compare, let’s suppose we want to report the results from two hypothetical polls regarding browser preference taken in 2000 and then 2015. For each year, we are simply comparing five quantities – the five percentages. A widely used graphical representation of percentages, popularized by Microsoft Excel, is the pie chart:

Introduction to Data Science - 9 Data visualization principles (1)

Here we are representing quantities with both areas and angles, since both the angle and area of each pie slice are proportional to the quantity the slice represents. This turns out to be a sub-optimal choice since, as demonstrated by perception studies, humans are not good at precisely quantifying angles and are even worse when area is the only available visual cue. The donut chart is an example of a plot that uses only area:

Introduction to Data Science - 9 Data visualization principles (2)

To see how hard it is to quantify angles and area, note that the rankings and all the percentages in the plots above changed from 2000 to 2015. Can you determine the actual percentages and rank the browsers’ popularity? Can you see how the percentages changed from 2000 to 2015? It is not easy to tell from the plot.

In this case, simply showing the numbers is not only clearer, but would also saves on printing costs if printing a paper copy:

Browser20002015
Opera32
Safari2122
Firefox2321
Chrome2629
IE2827

The preferred way to plot these quantities is to use length and position as visual cues, since humans are much better at judging linear measures. The barplot uses this approach by using bars of length proportional to the quantities of interest. By adding horizontal lines at strategically chosen values, in this case at every multiple of 10, we ease the visual burden of quantifying through the position of the top of the bars. Compare and contrast the information we can extract from the two figures.

Introduction to Data Science - 9 Data visualization principles (3)

Notice how much easier it is to see the differences in the barplot. In fact, we can now determine the actual percentages by following a horizontal line to the x-axis.

If for some reason you need to make a pie chart, label each pie slice with its respective percentage so viewers do not have to infer them from the angles or area:

Introduction to Data Science - 9 Data visualization principles (4)

In general, when displaying quantities, position and length are preferred over angles and/or area. Brightness and color are even harder to quantify than angles. But, as we will see later, they are sometimes useful when more than two dimensions must be displayed at once.

9.2 Know when to include 0

When using length as a visual cue, it is misinformative not to start the bars at 0. This is because, by using length as a visual cue, say with a barplot, we are implying the length is proportional to the quantities being displayed. By avoiding 0, relatively small differences can be made to look much bigger than they actually are. This approach is often used by politicians or media organizations trying to exaggerate a difference. Below is an illustrative example6:

Introduction to Data Science - 9 Data visualization principles (5)

(Source: Fox News, via Media Matters7.)

From the plot above, it appears that apprehensions have almost tripled when, in fact, they have only increased by about 16%. Starting the graph at 0 illustrates this clearly:

Introduction to Data Science - 9 Data visualization principles (6)

Here is another example:

Introduction to Data Science - 9 Data visualization principles (7)

(Source: Fox News, via Flowing Data8.)

This plot makes a 13% increase look like a five fold change. Here is the appropriate plot:

Introduction to Data Science - 9 Data visualization principles (8)

Finally, here is an extreme example that makes a very small difference of under 2% look like a 10-100 fold change:

Introduction to Data Science - 9 Data visualization principles (9)

(Source: Venezolana de Televisión via El Mundo9.)

Here is the appropriate plot:

Introduction to Data Science - 9 Data visualization principles (10)

When using position rather than length, it is then not necessary to include 0. This is particularly the case when we want to compare differences between groups relative to the within-group variability. Here is an illustrative example showing country average life expectancy stratified across continents in 2012:

Introduction to Data Science - 9 Data visualization principles (11)

Note that in the plot on the left, which includes 0, the space between 0 and 43 adds no information and makes it harder to compare the between and within group variability.

9.3 Do not distort quantities

During President Barack Obama’s 2011 State of the Union Address, the following chart was used to compare the US GDP to the GDP of four competing nations:

Introduction to Data Science - 9 Data visualization principles (12)

(Source: The 2011 State of the Union Address10)

Judging by the area of the circles, the US appears to have an economy over five times larger than China’s and over 30 times larger than France’s. However, if we look at the actual numbers, we see that this is not the case. The actual ratios are 2.6 and 5.8 times bigger than China and France, respectively. The reason for this distortion is that the radius, rather than the area, was made to be proportional to the quantity, which implies that the proportion between the areas is squared: 2.6 turns into 6.5 and 5.8 turns into 34.1. Here is a comparison of the circles we get if we make the value proportional to the radius and to the area:

Introduction to Data Science - 9 Data visualization principles (13)

Not surprisingly, ggplot2 defaults to using area rather than radius. Of course, in this case, we really should not be using area at all since we can use position and length:

Introduction to Data Science - 9 Data visualization principles (14)

9.4 Order categories by a meaningful value

When one of the axes is used to show categories, as is done in barplots and boxplots, the default ggplot2 behavior is to order the categories alphabetically when they are defined by character strings. If they are defined by factors, they are ordered by the factor levels. We rarely want to use alphabetical order. Instead, we should order by a meaningful quantity. In all the cases above, the barplots were ordered by the values being displayed. The exception was the graph showing barplots comparing browsers. In this case, we kept the order the same across the barplots to ease the comparison. Specifically, instead of ordering the browsers separately in the two years, we ordered both years by the average value of 2000 and 2015.

To appreciate how the right order can help convey a message, suppose we want to create a plot to compare the murder rate across states. We are particularly interested in the most dangerous and safest states. Note the difference when we order alphabetically (the default) versus when we order by the actual rate:

Here is an example showing boxplots of income distributions across regions. Here are the two versions plotted against each other:

Introduction to Data Science - 9 Data visualization principles (16)

The first orders the regions alphabetically, while the second orders them by the group’s median.

9.5 Show the data

We have focused on displaying single quantities across categories. We now shift our attention to displaying data, with a focus on comparing groups.

To motivate the principle, “show the data”, we go back to our artificial example of describing heights to ET, an extraterrestrial. This time let’s assume ET is interested in the difference in heights between males and females. A commonly seen plot used for comparisons between groups, popularized by software such as Microsoft Excel, is the dynamite plot, which shows the average and standard errors (standard errors are defined in a later chapter, but do not confuse them with the standard deviation of the data). The plot looks like this:

Introduction to Data Science - 9 Data visualization principles (17)

The average of each group is represented by the top of each bar and the antennae extend out from the average to the average plus two standard errors. If all ET receives is this plot, he will have little information on what to expect if he meets a group of human males and females. The bars go to 0: does this mean there are tiny humans measuring less than one foot? Are all males taller than the tallest females? Is there a range of heights? ET can’t answer these questions since we have provided almost no information on the height distribution.

This brings us the “show the data” principle. This simple ggplot2 code already generates a more informative plot than the barplot by simply showing all the data points:

Introduction to Data Science - 9 Data visualization principles (18)

For example, this plot gives us an idea of the range of the data. However, this plot has limitations as well, since we can’t really see all the 238 and 812 points plotted for females and males, respectively, and many points are plotted on top of each other. As we have previously described, visualizing the distribution is much more informative. But before doing this, we point out two ways we can improve a plot showing all the points.

The first is to add jitter, which adds a small random shift to each point. In this case, adding horizontal jitter does not alter the interpretation, since the point heights do not change, but we minimize the number of points that fall on top of each other and, therefore, get a better visual sense of how the data is distributed. A second improvement comes from using alpha blending: making the points somewhat transparent. The more points fall on top of each other, the darker the plot, which also helps us get a sense of how the points are distributed. Here is the same plot with jitter and alpha blending:

heights |>  ggplot(aes(sex, height)) + geom_jitter(width = 0.05, alpha = 0.2) 

Introduction to Data Science - 9 Data visualization principles (19)

Now we start getting a sense that, on average, males are taller than females. We also note dark horizontal bands of points, demonstrating that many report values that are rounded to the nearest integer.

9.6 Ease comparisons

9.6.1 Use common axes

Since there are so many points, it is more effective to show distributions rather than individual points. We therefore show histograms for each group:

Introduction to Data Science - 9 Data visualization principles (20)

However, from this plot it is not immediately obvious that males are, on average, taller than females. We have to look carefully to notice that the x-axis has a higher range of values in the male histogram. An important principle here is to keep the axes the same when comparing data across two plots. Below we see how the comparison becomes a little easier:

Introduction to Data Science - 9 Data visualization principles (21)

9.6.2 Align plots vertically to see horizontal changes and horizontally to see vertical changes

In these histograms, the visual cue related to decreases or increases in height are shifts to the left or right, respectively: horizontal changes. Aligning the plots vertically helps us see this change when the axes are fixed:

Introduction to Data Science - 9 Data visualization principles (22)

This plot makes it much easier to notice that men are, on average, taller.

If we want the more compact summary provided by boxplots, we then align them horizontally since, by default, boxplots move up and down with changes in height. Following our show the data principle, we then overlay all the data points:

Introduction to Data Science - 9 Data visualization principles (23)

Now contrast and compare these three plots, based on exactly the same data:

Introduction to Data Science - 9 Data visualization principles (24)

Notice how much more we learn from the two plots on the right. Barplots are useful for showing one number, but not very useful when we want to describe distributions.

9.7 Consider transformations

We have motivated the use of the log transformation in cases where the changes are multiplicative. Population size was an example in which we found a log transformation to yield a more informative transformation.

The combination of an incorrectly chosen barplot and a failure to use a log transformation when one is merited can be particularly distorting. As an example, consider this barplot showing the average population sizes for each continent in 2015:

Introduction to Data Science - 9 Data visualization principles (25)

From this plot, one would conclude that countries in Asia are much more populous than in other continents. Following the show the data principle, we quickly notice that this is due to two very large countries, which we assume are India and China:

Introduction to Data Science - 9 Data visualization principles (26)

Using a log transformation here provides a much more informative plot. We compare the original barplot to a boxplot using the log scale transformation for the y-axis:

Introduction to Data Science - 9 Data visualization principles (27)

With the new plot, we realize that countries in Africa actually have a larger median population size than those in Asia.

Other transformations you should consider are the logistic transformation (logit), useful to better see fold changes in odds, and the square root transformation (sqrt), useful for count data.

9.8 Visual cues to be compared should be adjacent

For each continent, let’s compare income in 1970 versus 2010. When comparing income data across regions between 1970 and 2010, we made a figure similar to the one below, but this time we investigate continents rather than regions.

Introduction to Data Science - 9 Data visualization principles (28)

The default in ggplot2 is to order labels alphabetically so the labels with 1970 come before the labels with 2010, making the comparisons challenging because a continent’s distribution in 1970 is visually far from its distribution in 2010. It is much easier to make the comparison between 1970 and 2010 for each continent when the boxplots for that continent are next to each other:

Introduction to Data Science - 9 Data visualization principles (29)

The comparison becomes even easier to make if we use color to denote the two things we want to compare:

Introduction to Data Science - 9 Data visualization principles (30)

9.9 Think of the color blind

About 10% of the population is color blind. Unfortunately, the default colors used in ggplot2 are not optimal for this group. However, ggplot2 does make it easy to change the color palette used in the plots. An example of how we can use a color blind friendly palette is described in the R cookbook11:

color_blind_friendly_cols <-  c("#999999", "#E69F00", "#56B4E9", "#009E73",  "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

Here are the colors

Introduction to Data Science - 9 Data visualization principles (31)

There are several resources that can help you select colors, for example tutorials on R-bloggers12.

9.10 Plots for two variables

In general, you should use scatterplots to visualize the relationship between two variables. In every single instance in which we have examined the relationship between two variables, including total murders versus population size and life expectancy versus fertility rates, we have used scatterplots. This is the plot we generally recommend. However, there are some exceptions and we describe two alternative plots here: the slope chart and the Bland-Altman plot.

9.10.1 Slope charts

One exception where another type of plot may be more informative is when you are comparing variables of the same type, but at different time points and for a relatively small number of comparisons. For example, comparing life expectancy between 2010 and 2015. In this case, we might recommend a slope chart.

There is no geometry for slope charts in ggplot2, but we can construct one using geom_line. We need to do some tinkering to add labels. Below is an example comparing 2010 to 2015 for large western countries:

Introduction to Data Science - 9 Data visualization principles (32)

An advantage of the slope chart is that it permits us to quickly get an idea of changes based on the slope of the lines. Although we are using angle as the visual cue, we also have position to determine the exact values. Comparing the improvements is a bit harder with a scatterplot:

Introduction to Data Science - 9 Data visualization principles (33)

In the scatterplot, we have followed the principle use common axes since we are comparing these before and after. However, if we have many points, slope charts stop being useful as it becomes hard to see all the lines.

9.10.2 Bland-Altman plot

Since we are primarily interested in the difference, it makes sense to dedicate one of our axes to it. The Bland-Altman plot, also known as the Tukey mean-difference plot and the MA-plot, shows the difference versus the average:

Introduction to Data Science - 9 Data visualization principles (34)

Here, by simply looking at the y-axis, we quickly see which countries have shown the most improvement. We also get an idea of the overall value from the x-axis.

9.11 Encoding a third variable

An earlier scatterplot showed the relationship between infant survival and average income. Below is a version of this plot that encodes three additional variables: OPEC membership, region, and population.

Introduction to Data Science - 9 Data visualization principles (35)

We encode categorical variables with color and shape. These shapes can be controlled with shape argument. Below are the shapes available for use in R. For the last five, the color goes inside.

Introduction to Data Science - 9 Data visualization principles (36)

For continuous variables, we can use color, intensity, or size. We now show an example of how we do this with a case study.

When selecting colors to quantify a numeric variable, we choose between two options: sequential and diverging. Sequential colors are suited for data that goes from high to low. High values are clearly distinguished from low values. Here are some examples offered by the package RColorBrewer:

Introduction to Data Science - 9 Data visualization principles (37)

Diverging colors are used to represent values that diverge from a center. We put equal emphasis on both ends of the data range: higher than the center and lower than the center. An example of when we would use a divergent pattern would be if we were to show height in standard deviations away from the average. Here are some examples of divergent patterns:

Introduction to Data Science - 9 Data visualization principles (38)

9.12 Avoid pseudo-three-dimensional plots

The figure below, taken from the scientific literature13, shows three variables: dose, drug type and survival. Although your screen/book page is flat and two-dimensional, the plot tries to imitate three dimensions and assigned a dimension to each variable.

Introduction to Data Science - 9 Data visualization principles (39)

(Image courtesy of Karl Broman)

Humans are not good at seeing in three dimensions (which explains why it is hard to parallel park) and our limitation is even worse with regard to pseudo-three-dimensions. To see this, try to determine the values of the survival variable in the plot above. Can you tell when the purple ribbon intersects the red one? This is an example in which we can easily use color to represent the categorical variable instead of using a pseudo-3D:

Introduction to Data Science - 9 Data visualization principles (40)

Notice how much easier it is to determine the survival values.

Pseudo-3D is sometimes used completely gratuitously: plots are made to look 3D even when the 3rd dimension does not represent a quantity. This only adds confusion and makes it harder to relay your message. Here are two examples:

Introduction to Data Science - 9 Data visualization principles (41)

Introduction to Data Science - 9 Data visualization principles (42)

(Images courtesy of Karl Broman)

9.13 Avoid too many significant digits

By default, statistical software like R returns many significant digits. The default behavior in R is to show 7 significant digits. That many digits often adds no information and the added visual clutter can make it hard for the viewer to understand the message. As an example, here are the per 10,000 disease rates, computed from totals and population in R, for California across the five decades:

stateyearMeaslesPertussisPolio
California194037.882632018.33978610.8266512
California195013.91242054.74673501.9742639
California196014.1386471NA0.2640419
California19700.9767889NANA
California19800.37434670.0515466NA

We are reporting precision up to 0.00001 cases per 10,000, a very small value in the context of the changes that are occurring across the dates. In this case, one significant figures is enough and clearly makes the point that rates are decreasing:

stateyearMeaslesPertussisPolio
California194037.918.30.8
California195013.94.72.0
California196014.1NA0.3
California19701.0NANA
California19800.40.1NA

Useful ways to change the number of significant digits or to round numbers are signif and round. You can define the number of significant digits globally by setting options like this: options(digits = 3).

Another principle related to displaying tables is to place values being compared on columns rather than rows. Note that our table above is easier to read than this one:

statedisease19401950196019701980
CaliforniaMeasles37.913.914.110.4
CaliforniaPertussis18.34.7NANA0.1
CaliforniaPolio0.82.00.3NANA

9.14 Know your audience

Graphs can be used 1) for our own exploratory data analysis, 2) to convey a message to experts, or 3) to help convey a message to a general audience. Make sure that the intended audience understands each element of the plot.

As a simple example, consider that for your own exploration it may be more useful to log-transform data and then plot it. However, for a general audience that is unfamiliar with converting logged values back to the original measurements, using a log-scale for the axis instead of log-transformed values will be much easier to digest.

9.15 Exercises

For these exercises, we will be using the vaccines data in the dslabs package:

library(dslabs)

1. Pie charts are appropriate:

  1. When we want to display percentages.
  2. When ggplot2 is not available.
  3. When I am in a bakery.
  4. Never. Barplots and tables are always better.

2. What is the problem with the plot below:

Introduction to Data Science - 9 Data visualization principles (43)

  1. The values are wrong. The final vote was 306 to 232.
  2. The axis does not start at 0. Judging by the length, it appears Trump received 3 times as many votes when, in fact, it was about 30% more.
  3. The colors should be the same.
  4. Percentages should be shown as a pie chart.

3. Take a look at the following two plots. They show the same information: 1928 rates of measles across the 50 states.

Introduction to Data Science - 9 Data visualization principles (44)

Which plot is easier to read if you are interested in determining which are the best and worst states in terms of rates, and why?

  1. They provide the same information, so they are both equally as good.
  2. The plot on the right is better because it orders the states alphabetically.
  3. The plot on the right is better because alphabetical order has nothing to do with the disease and by ordering according to actual rate, we quickly see the states with most and least rates.
  4. Both plots should be a pie chart.

4. To make the plot on the left, we have to reorder the levels of the states’ variables.

dat <- us_contagious_diseases |>  filter(year == 1967 & disease=="Measles" & !is.na(population)) |> mutate(rate = count / population * 10000 * 52 / weeks_reporting)

Note what happens when we make a barplot:

dat |> ggplot(aes(state, rate)) + geom_col() + coord_flip() 

Introduction to Data Science - 9 Data visualization principles (45)

Define these objects:

state <- dat$staterate <- dat$count/dat$population*10000*52/dat$weeks_reporting

Redefine the state object so that the levels are re-ordered. Print the new object state and its levels so you can see that the vector is not re-ordered by the levels.

5. Now with one line of code, define the dat table as done above, but change the use mutate to create a rate variable and re-order the state variable so that the levels are re-ordered by this variable. Then make a barplot using the code above, but for this new dat.

6. Say we are interested in comparing gun homicide rates across regions of the US. We see this plot:

library(dslabs)murders |> mutate(rate = total/population*100000) |>group_by(region) |>summarize(avg = mean(rate)) |>mutate(region = factor(region)) |>ggplot(aes(region, avg)) +geom_col() + ylab("Murder Rate Average")

Introduction to Data Science - 9 Data visualization principles (46)

and decide to move to a state in the western region. What is the main problem with this interpretation?

  1. The categories are ordered alphabetically.
  2. The graph does not show standard errors.
  3. It does not show all the data. We do not see the variability within a region and it’s possible that the safest states are not in the West.
  4. The Northeast has the lowest average.

7. Make a boxplot of the murder rates defined as

murders |> mutate(rate = total/population*100000)

by region, showing all the points and ordering the regions by their median rate.

8. The plots below show three continuous variables.

Introduction to Data Science - 9 Data visualization principles (47)

The line \(x=2\) appears to separate the points. But it is actually not the case, which we can see by plotting the data in a couple of two-dimensional points.

Introduction to Data Science - 9 Data visualization principles (48)

Why is this happening?

  1. Humans are not good at reading pseudo-3D plots.
  2. There must be an error in the code.
  3. The colors confuse us.
  4. Scatterplots should not be used to compare two variables when we have access to 3.
  1. http://kbroman.org/↩︎

  2. https://www.biostat.wisc.edu/~kbroman/presentations/graphs2017.pdf↩︎

  3. https://github.com/kbroman/Talk_Graphs↩︎

  4. https://www.peteraldhous.com/ucb/2014/dataviz/index.html↩︎

  5. https://github.com/rafalab/dsbook-part-1/blob/main/dataviz/dataviz-principles.qmd↩︎

  6. https://www.peteraldhous.com/ucb/2014/dataviz/week2.html↩︎

  7. http://mediamatters.org/blog/2013/04/05/fox-news-newest-dishonest-chart-immigration-enf/193507↩︎

  8. http://flowingdata.com/2012/08/06/fox-news-continues-charting-excellence/↩︎

  9. https://www.elmundo.es/america/2013/04/15/venezuela/1366029653.html↩︎

  10. https://www.youtube.com/watch?v=kl2g40GoRxg↩︎

  11. http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette↩︎

  12. https://www.r-bloggers.com/2013/10/creating-colorblind-friendly-figures/↩︎

  13. https://projecteuclid.org/download/pdf_1/euclid.ss/1177010488↩︎

Introduction to Data Science - 9  Data visualization principles (2024)
Top Articles
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 5572

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.