# Lecture 5: Data Visualization and Exploratory Analysis

## Statistics / Biostatistics

---

## Learning Objectives

By the end of this lecture, students will be able to:

1. Construct and interpret histograms, boxplots, bar charts, and scatterplots
2. Use stem-and-leaf plots and frequency tables for exploratory analysis
3. Identify outliers and distributional features from graphical displays
4. Select the appropriate graph type for different data types
5. Recognize common pitfalls in data visualization

---

## Lecture Content

### I. The Importance of Data Visualization

Graphs reveal patterns, trends, and anomalies that raw numbers may obscure. Exploratory data analysis (EDA), a term coined by John Tukey in 1977, should precede any formal statistical testing. Visualization helps researchers assess distributional shape (symmetry, skewness, modality), identify outliers and data entry errors, evaluate relationships between variables, and communicate findings effectively to clinical audiences. Skipping the visualization step risks applying inappropriate tests and missing important features of the data.

### II. Frequency Tables and Stem-and-Leaf Plots

A **frequency table** organizes data into classes (bins) and counts the number of observations in each class. It typically includes columns for the class, frequency, relative frequency (proportion), and cumulative frequency. The choice of bin width affects the table's appearance; too few or too many bins can be misleading, so care should be taken to balance resolution with clarity.

A **stem-and-leaf plot** is a simple display that preserves individual data values. The "stem" represents the leading digit(s) while the "leaf" represents the trailing digit. For example, for data values 72, 75, 78, 81, and 83, the stems are 7 and 8 and the leaves are 2, 5, 8 and 1, 3 respectively. The advantage of this plot is that it retains the actual data values while simultaneously showing the shape of the distribution. It is best suited to small-to-moderate datasets.

### III. Histograms

A histogram is a bar chart for continuous data in which bars represent the frequency (or relative frequency) of data within intervals. The bars are adjacent with no gaps, reflecting the continuous nature of the variable, and the area of each bar is proportional to the frequency in that interval. The shape of the histogram reveals whether the distribution is symmetric, skewed, bimodal, or uniform.

When constructing histograms, an appropriate number of bins should be chosen (Sturges' rule suggests k = 1 + 3.322 * log10(n)), and bins should be of equal width for easy interpretation. Density histograms, where the y-axis represents relative frequency divided by bin width, allow meaningful comparison across different sample sizes.

<image>A four-panel figure showing histograms of different distribution shapes using clinical data. Panel A: Symmetric distribution (e.g., adult heights). Panel B: Right-skewed distribution (e.g., hospital costs). Panel C: Bimodal distribution (e.g., age at diagnosis for a condition affecting both children and elderly). Panel D: Uniform distribution (e.g., random number generator output). Each panel has labeled axes and a brief caption describing the clinical context.</image>

### IV. Boxplots (Box-and-Whisker Plots)

A boxplot displays the five-number summary: minimum, Q1, median, Q3, and maximum. The box extends from Q1 to Q3 and contains the middle 50% of the data, with a line inside marking the median. Whiskers extend to the most extreme values within 1.5 * IQR of the box, and individual points beyond the whiskers are plotted as potential outliers.

Boxplots offer several advantages: they provide a compact summary of the distribution, allow easy side-by-side comparison of multiple groups, and clearly highlight outliers and skewness. A modified boxplot may additionally show the mean as a distinct symbol (such as a diamond or cross) alongside the median line.

### V. Bar Charts and Pie Charts

**Bar charts** display frequencies or proportions for categorical variables. Unlike histograms, the bars are separated by gaps to reflect the discrete nature of the categories. They can be vertical or horizontal, grouped to compare categories across groups, or stacked to show composition within categories.

**Pie charts** show proportions as slices of a circle and work best when limited to a small number of categories (three to five). However, the human eye finds it harder to compare areas than lengths, so pie charts are generally discouraged in scientific publications in favor of bar charts, which convey the same information more precisely.

<image>A side-by-side comparison of visualization options for the same categorical dataset (e.g., distribution of blood types in a study sample). Panel A: A vertical bar chart with frequency on the y-axis and blood type on the x-axis. Panel B: A pie chart showing the same data as proportional slices. Panel C: A horizontal bar chart sorted by frequency. Below each, a brief note on strengths and weaknesses of each format.</image>

### VI. Scatterplots

Scatterplots display the relationship between two continuous variables, with each point representing one observation's (x, y) pair. They are used to assess the direction of association (positive, negative, or none), the strength of association (tight cluster versus dispersed cloud), the linearity or nonlinearity of the relationship, and the presence of outliers or influential points. A trend line, such as a linear regression line, can be added to summarize the relationship. For example, a scatterplot of patient age versus systolic blood pressure might reveal a positive, roughly linear trend.

### VII. Line Graphs and Time Series Plots

Line graphs are used to display data collected over time, with the x-axis typically representing time and the y-axis representing the measured variable. Data points are connected with lines to highlight trends. They are useful for tracking disease incidence over years, monitoring a patient's lab values over time, or displaying survival curves such as Kaplan-Meier estimates. Multiple lines can be plotted to compare groups over time.

### VIII. Common Pitfalls in Data Visualization

Several common errors can make graphs misleading. A **truncated y-axis** that starts at a value other than zero can exaggerate differences. **Misleading scales**, where different scales are used for compared groups, distort comparisons. **3D effects** warp the perception of bar heights and areas and should be avoided in scientific graphs. **Cherry-picking time intervals** by selecting start and end points to create a desired impression is another common pitfall, as is **overcrowding** a plot with too many data series or categories. Pie charts with too many slices become unreadable.

Best practices for data visualization include labeling all axes clearly with units, providing a descriptive title or caption, using consistent scales for comparisons, and showing data points when possible rather than summary bars alone.

<image>A "bad vs. good" visualization comparison. Panel A (Bad): A 3D bar chart with a truncated y-axis, no axis labels, and garish colors, showing drug efficacy data that appears to show a huge difference. Panel B (Good): The same data displayed as a simple 2D bar chart starting at zero, with clear axis labels, error bars, and individual data points overlaid, revealing that the actual difference is modest. Annotations highlight each design flaw corrected.</image>

### IX. Exploratory Data Analysis Workflow

A systematic EDA workflow proceeds in steps. First, examine each variable individually through univariate analysis: use frequency tables, histograms, and boxplots for continuous variables and bar charts for categorical variables. Second, examine relationships between variables through bivariate analysis: use scatterplots for two continuous variables, side-by-side boxplots for a continuous outcome by group, and cross-tabulations for two categorical variables. Third, identify data quality issues, including missing data patterns, implausible values that may be data entry errors, and outliers warranting further investigation. Finally, summarize the findings to guide the choice of formal statistical analysis methods.

---
