Calculating and Graphing Standard Deviation

By Annabella Tyrrell · Published 25 November 2026
graphicsstata

Two groups of cars both average 20 miles per gallon. The standard deviation measures how spread out the individual values are around their mean.

Select Two Groups of Cars

Loading Stata’s built-in auto dataset, we select three cars with fuel economies of 18, 20 and 22 mpg for Group A and three with 12, 20 and 28 mpg for Group B. This gives the groups the same mean so we can compare their spread directly:

sysuse auto, clear

keep if inlist(mpg, 12, 18, 20, 22, 28)

bysort mpg (make): generate pick = _n

keep if pick == 1 | (mpg == 20 & pick == 2)

generate group = 1 if inlist(mpg, 18, 22) | (mpg == 20 & pick == 1)

replace group = 2 if missing(group)

label define car_group 1 "Group A" 2 "Group B"

label values group car_group

Where several cars have the same mileage, pick numbers them alphabetically by model. We keep one car at each mileage, except at 20 mpg, where we keep two so each group has its own car.

Compare the Standard Deviations

Use tabstat to show the mean and sample standard deviation (sd) side by side:

tabstat mpg, by(group) statistics(mean sd min max) nototal

Summary for variables: mpg
Group variable: group

Group A has a standard deviation of 2 mpg, while Group B has a standard deviation of 8 mpg.

Check the Calculation

For a group of n observations with mean x̄, the sample standard deviation is:

s = ∑ i=1 n ( xi − x¯ ) 2 n−1

For Group A, the three differences from the mean are −2, 0 and 2 mpg. We can check both groups using display:

display "Group A SD = " sqrt(((18-20)^2 + (20-20)^2 + (22-20)^2) / (3-1))
Group A SD = 2

display "Group B SD = " sqrt(((12-20)^2 + (20-20)^2 + (28-20)^2) / (3-1))
Group B SD = 8

Squaring the differences keeps values below and above the mean from cancelling each other out, then taking the square root brings the result back to miles per gallon.

Graph the Spread Around the Mean

The row variable positions Group A above Group B, with the cars ordered from lowest to highest mileage within each group:

bysort group (mpg): generate row = cond(group == 1, 9, 4) - _n

bysort group: egen mean_mpg = mean(mpg)

bysort group: egen sd_mpg = sd(mpg)

generate sd_end = mean_mpg + sd_mpg

generate sd_row = row - 1 if mpg == 20

generate sd_label = string(sd_mpg, "%1.0f") + " mpg" if !missing(sd_row)

Use rspike to draw a horizontal line from the mean to each car’s mileage, then place a dot at the end with scatter. The rbar layer draws the green standard deviation bar on its own row beneath each group. In this example, each group has one car at 20 mpg, so we use that observation to draw the bar once per group:

bysort group: egen bottom_row = min(row)

replace sd_row = bottom_row - 1 if !missing(sd_row)
(2 real changes made)

quietly summarize mean_mpg, meanonly

local shared_mean = r(mean)

twoway ///
     (rspike mean_mpg mpg row, horizontal lcolor(gs9)) ///
     (scatter row mpg, msymbol(O) mcolor(blue) msize(medium)) ///
     (rbar mean_mpg sd_end sd_row if !missing(sd_row), ///
         horizontal barwidth(0.28) color(green)) ///
     (scatter sd_row sd_end if !missing(sd_row), ///
         msymbol(none) mlabel(sd_label) mlabposition(3) mlabcolor(green)), ///
     xline(shared_mean', lcolor(red) lpattern(dash)) ///
     xlabel(12(2)28) xscale(range(11 31)) ///
     ylabel(none) yscale(range(-1 10) off) ///
     text(9 12 "Group A" 4 12 "Group B", placement(e)) ///
     text(9.8 shared_mean' "Mean = `shared_mean' mpg", color(red)) ///
     xtitle("Fuel economy (miles per gallon)") ytitle("") ///
     title("Standard deviation around a shared mean") ///
     legend(order(2 "Individual car" 3 "One standard deviation") ///
         rows(1) position(6))

In Group A, the cars range from 18 to 22 mpg, and the green bar extends 2 mpg from the mean. In Group B, the cars range from 12 to 28 mpg, and the green bar extends 8 mpg.

6 views 1 found this useful