* First, read the data clear import delimited "Z:\PUBLIC_web\Stataworkshops\Grammar of Graphics\ISISattacks.csv" generate attack = date(date, "MDY") format %tdMon_dd,_CCYY attack * Next figure out the graphical objects. * At first blush, this might seem like a job for "graph dot", * we have one categorical and one continuous axis graph dot attack, over(location) asyvars over(role) legend(off) exclude0 * We have to know that "over() asyvars" allows us to stack dots within * a second "over()" set of categories, and "legend(off)" and * "exlude0" allow us to see a reasonable graph area. * But we can't graph the dates "asis" - location and role do not identify * unique points - and we have no ability to resize the dots. Nice * idea, but it turns into a dead end. * As often happens, we will turn to "graph twoway scatter" instead. This means * we will have to do more work to create our graph-data. * We need the categorical variable to be encoded numerically. encode role, generate(ISIS) label(rolelbl) * We'll want separate variables for injuries versus deaths separate ISIS, by(dead < .) label variable ISIS0 "injuries only" label variable ISIS1 "sized by number of deaths" label values ISIS? rolelbl * And we'll use number of deaths to size the points, so we need a non-missing value * to use with the injuries indicator. replace dead = 1 if dead ==. * Geometry, overlaying injured and dead. * Weights give us proportionally sized markers scatter ISIS? attack [w=dead] * Better y coordinates and guide make it easier to see we are on the right track. * (Note we cannot suppress the y-ticks if we have y-labels.) scatter ISIS? attack [w=dead], /// yscale(range(0.5 3.5) reverse noline) /*ytick(none)*/ /// ylabel(1(1)3, valuelabel angle(horizontal)) * A few similar options give us a better xaxis and guide, and aspect ratio scatter ISIS? attack [w=dead], /// yscale(range(0.5 3.5) reverse noline) /// ylabel(1(1)3, valuelabel angle(horizontal)) /// xtitle("") xscale(alt noline) tscale(range(1Sep2014 1Feb2017)) /// ysize(2) xsize(6) * We cannot simply add marker labels, they cannot be used in * layers that have weights ~= 1. * Here we lack the labels we want, and do not have the ones we do. scatter ISIS? attack [w=dead], /// yscale(range(0.5 3.5) reverse noline) /// ylabel(1(1)3, valuelabel angle(horizontal)) /// xtitle("") xscale(alt noline) tscale(range(1Sep2014 1Feb2017)) /// ysize(2) xsize(6) /// mlabel(location) mlabposition(6) * So we will overlay our scatter plot, with a scatter plot of just labels * We need to calculate a position for these labels generate isis2 = ISIS1 + 0.4 replace isis2 = ISIS1 if location == "Beirut" * And select just certain labels to use generate location2 = location if dead >= 14 * overlaid twoway (scatter ISIS1 ISIS0 attack [w=dead]) /// (scatter isis2 attack, /// msymbol(none) mlabel(location2) mlabposition(0)) /// , yscale(range(0.5 3.5) reverse noline) /// ylabel(1(1)3, valuelabel angle(horizontal)) /// xtitle("") xscale(alt noline) tscale(range(1Sep2014 1Feb2017)) /// ysize(2) xsize(6) * Finally, we can clean up color schemes, legend, title, and notes twoway (scatter ISIS1 ISIS0 attack [w=dead], /// mfcolor(red*.5 gray) mlcolor(gs50 gs50) mlwidth(thin thin)) /// (scatter isis2 attack, /// msymbol(none) mlabel(location2) mlabposition(0) mlabcolor(black)) /// , yscale(range(0.5 3.5) reverse noline) /*ytick(none)*/ /// ylabel(1(1)3, valuelabel angle(horizontal)) /// xtitle("") xscale(alt noline) tscale(range(1Sep2014 1Feb2017)) /// ysize(2) xsize(6) /// legend(order(1 2) position(12) region(style(none))) /// title("ISIS attacks, outside of its self-proclaimed caliphate") /// note("Recreated from: www.nytimes.com/interactive/2017/02/04/world/isis-remote-control-enabled-attack.html") * If you wanted to continue, you might: * You might relabel the time line. * You could also come up with a little tighter position for the location labels, * which vary in the NYTimes original. * A final quality that we cannot mimic in Stata is the "transparency", the way * the overlaid markers add to the intensity of color. In Stata we simply * have "the last ink applied, wins".