This time, we are going to play with a Dumbbell chart (a variant of Cleveland’s dotplot, which is usually best suited for time-varying data) that I produced in R some years ago. Some variations around this kind of plot can be found in my R retrospective.
In R, it was quite simple:
data(birthwt, package = "MASS")
birthwt$lwt <- birthwt$lwt * 0.45
# -%<--
d <- aggregate(low ~ race + cut2(lwt, g = 3) + smoke, data = birthwt, sum)
d$pct <- d$low / nrow(birthwt)
names(d)[2] <- "age"
p <- ggplot(data = d, aes(x = pct, y = age)) +
geom_line(aes(group = age), color = grey(.7)) +
geom_point(aes(color = smoke)) +
scale_color_manual("", values = c("steelblue", "orange")) +
facet_wrap(~race, ncol = 3) +
labs(x = "Proportion weight < 2.5 kg", y = "Mother age")
The cut2 function is from the Hmisc package, and the rest of the code is just
ggplot2 basic stuff. Here’s the Stata version, which is way more verbose and
took me a lot more time:1

And here’s the code:
webuse lbw, clear
replace lwt = lwt * 0.45
* -%<--
preserve
xtile lwt3 = lwt, nq(3)
centile lwt, centile(0 `=100/3' `=200/3' 100)
label define lwt3_lbl 1 "[`:di %3.1f r(c_1)', `:di %3.1f r(c_2)')" ///
2 "[`:di %3.1f r(c_2)', `:di %3.1f r(c_3)')" ///
3 "[`:di %3.1f r(c_3)', `:di %4.1f r(c_4)']"
label values lwt3 lwt3_lbl
gen obs_total = _N
collapse (sum) low (first) obs_total, by(race lwt3 smoke)
gen pct = low / obs_total
reshape wide low pct, i(race lwt3) j(smoke)
twoway (rspike pct0 pct1 lwt3, horizontal lcolor(gs11) lwidth(medthick)) ///
(scatter lwt3 pct0, mcolor("70 130 180") msize(medium)) ///
(scatter lwt3 pct1, mcolor("255 165 0") msize(medium)), ///
by(race, cols(3) note("") graphregion(color(white)) ///
legend(position(3))) subtitle(, bcolor(none) lcolor(none)) ///
legend(order(2 "non-smoker" 3 "smoker") cols(1) region(lcolor(none))) ///
xtitle("Proportion weight < 2.5 kg") xlabel(0 `=2/100' `=4/100' `=6/100', format(%4.2f) ///
grid glcolor(gs14) glpattern(solid)) xscale(range(0 `=7/100')) ///
ytitle("Mother age") ylabel(1 2 3, valuelabel angle(0) noticks grid glcolor(gs14) glpattern(solid)) ///
yscale(range(`=5/10' `=35/10') noline) plotregion(lcolor(none))
restore
I first struggle with _pctile lwt, nq(3) and quietly summarize lwt to get
interval bounds. As an r-class command, _pctile was overwriting the r(min)
and r(max) values returned by summarize, and when I swapped the order of the
two commands – _pctile first, then summarize, I got the opposite behavior
and the r() return list was again wiped out. Finally, I decided to use
centile, although I believe egen lwt3 = cut(lwt), at(...) icodes label with
appropriate bounds should be fine as well.
The remaining code is standard commands: collapse as an equivalent to R’s
aggregate and the longish twoway incantation to pile up everything on the
same plotting region.
♪ New Order • Sub-Culture
I updated my Vim setup and I can now send code from MacVim directly to an
existing Stata window; if there’s no Stata window, the application is
launched automatically. In this case, it looks like MacVim doesn’t respect
my shell settings and makes use of a different LC_NUMERIC and there we go:
I got comma as a decimal separator which makes computation like lwt * 0.45
completely off (lwt is originally an int but should be promoted to a
float). ↩︎