Book contents

STATHARBOR ANALYTICS · INTRODUCTION TO STATISTICS · CHAPTER 26

Data Visualization

Data visualization communicates information clearly and effectively through graphical means. A well–chosen plot can reveal patterns, trends and outliers that might be missed in summary statistics. This chapter covers principles of good graphics and demonstrates common plot types using R, Python and Julia.

Principles of Good Graphics

Edward Tufte emphasised graphical integrity and minimalism. Good plots:

Common Plot Types

Histograms and Density Plots

Histograms display the distribution of a continuous variable by binning. Density plots are smooth estimates of the distribution.

Worked example

Histograms in R, Python and Julia.

hist(df$height, breaks=10, col="lightblue", main="Histogram of Height")
density_height <- density(df$height)
plot(density_height)
import matplotlib.pyplot as plt
import seaborn as sns

plt.hist(df['height'], bins=10, color='lightblue')
plt.title('Histogram of Height')
plt.show()

sns.kdeplot(df['height'])
plt.show()
using Plots
histogram(df.height, bins=10, xlabel="Height", title="Histogram of Height")
density(df.height, xlabel="Height", title="Density Plot")

Boxplots

Boxplots summarise distributions and identify outliers using the median and quartiles. They are useful for comparing groups.

Scatterplots and Trend Lines

Scatterplots show the relationship between two quantitative variables. Adding a trend line helps visualise patterns.

Bar Charts

Bar charts display counts or proportions of categorical variables. Stacked or grouped bar charts allow comparisons across categories.

Multivariate Graphics

When working with more than two variables, consider:

Exercises

Practice

  1. Create a boxplot of a quantitative variable grouped by a categorical variable (e.g., weight by gender). Interpret differences in medians and variability.

  2. For a dataset with three quantitative variables, produce a scatterplot matrix (pair plot) and identify any outliers or nonlinear relationships.

  3. Use ggplot2 (R) or seaborn (Python) to create a faceted histogram of a variable across different categories. Discuss any patterns.

  4. Design a visualization to compare the distribution of exam scores across three courses. Explain your choice of plot and how it meets the principles of good graphics.

References