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:
Accurately represent the data without distortion.
Use appropriate scales and labels.
Avoid unnecessary decorations (chart junk).
Highlight important patterns and comparisons.
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:
Scatterplot matrix: grid of scatterplots for all pairs of variables.
Heatmaps: colour–coded tables representing magnitude.
Faceting: splitting data into subsets and plotting each subset in its own panel.
Exercises
Practice
Create a boxplot of a quantitative variable grouped by a categorical variable (e.g., weight by gender). Interpret differences in medians and variability.
For a dataset with three quantitative variables, produce a scatterplot matrix (pair plot) and identify any outliers or nonlinear relationships.
Use ggplot2 (R) or seaborn (Python) to create a faceted histogram of a variable across different categories. Discuss any patterns.
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
Tufte ER. The Visual Display of Quantitative Information. Graphics Press; 1983.
Wickham H. ggplot2: Elegant Graphics for Data Analysis. 3rd ed. Springer; 2016.
Wilke CO. Fundamentals of Data Visualization. O’Reilly; 2019.