24  Scatter Plot and Histogram

24.1 Scatter Plot

A scatter plot (or scatter diagram) uses Cartesian coordinates to display values for two variables across a set of observations. Each point’s position on the horizontal axis reflects one variable, and its position on the vertical axis reflects the other.

Relationships: scatter plots are particularly useful for spotting the relationship or correlation between two variables — trends, clusters, and outliers all show up visually in a way a table can’t convey.

Correlation detection: they make it easy to see whether an increase in one variable tends to come with an increase in the other (positive correlation), a decrease (negative correlation), or no clear pattern at all.

Take rainfall and yield across a set of farms. Plotting rainfall on the x-axis and yield on the y-axis can reveal whether higher rainfall tends to come with higher yield — a positive correlation — or whether the relationship is weaker or more complex than that.

24.1.1 Dataset Example

Rainfall (mm) Yield (tons/ha)
540 2.65
610 2.90
780 3.55
905 3.90
920 4.20

We’ll visualize this data to see whether rainfall and yield move together.

24.1.2 R Code for a Scatter Plot

In R, the base plot() function creates a scatter plot directly.

The points trace a clear upward pattern — as rainfall increases across these five farms, so does yield, suggesting a positive relationship worth investigating further with a formal correlation or regression analysis (covered in later topics).


24.2 Histogram

A histogram is a graphical estimate of the distribution of a continuous numerical variable, first introduced by Karl Pearson. It groups values into contiguous ranges (bins) and represents the count of observations in each bin as the height of a bar.

Distribution: histograms reveal the shape of a numeric variable’s spread — roughly normal, skewed, or bimodal — which matters for choosing the right statistical test or model later.

Outliers and shape: they also make outliers and unusual clusters visible, which is critical before running statistical analyses that assume a particular distribution.

Consider yield (tons/ha) recorded across 12 farms in a district. A histogram shows how many farms fall into each yield range — useful for spotting whether most farms cluster around a typical yield or whether the district has a wide, uneven spread.

24.2.1 Dataset Example

Yield (tons/ha)
2.6
3.1
2.9
3.8
4.1
3.4
2.8
4.3
3.6
3.1
2.9
2.3

24.2.2 R Code for a Histogram

In R, the base hist() function creates a histogram.

This creates a histogram with 5 bins. Most farms in this small sample cluster between 2.8 and 3.6 tons/ha, with a handful of higher- and lower-yielding outliers — the kind of pattern worth investigating against irrigation type or soil quality.


24.3 3D Scatter Plot

A regular scatter plot handles two variables at once. When a third variable matters too — for example, how rainfall and fertilizer use together relate to yield — a 3D scatter plot adds a third (z) axis, letting all three relationships be explored in one interactive view. The plotly package supports this directly in R.

Rotating the plot (drag to orbit) makes it easier to judge whether higher rainfall and fertilizer use together, rather than either alone, best explain the higher-yield farms in the dataset.


Summary

Concept Description
Scatter Plot and Histogram
Scatter Plot Plots two variables against each other to reveal relationships, correlations, and outliers — e.g., rainfall against yield
Histogram Groups a numeric variable into bins to show the shape of its distribution — e.g., the spread of yield across farms
3D Scatter Plot Extends a scatter plot to three variables using an interactive z-axis, e.g., rainfall, fertilizer, and yield together