cd "Z:\PUBLIC_web\Stataworkshops\Grammer of Graphics\" import delimited "Iowa HuffingtonPost.csv", clear generate LV = strpos(pop, "LV") > 0 keep if LV // Just use "likely voter" polls, not "registered voters" // Convert data from string to a form useful for graphing generate polldate = date(substr(date,strpos(date, "-")+2,.)+"/2014", "MDY") format polldate %tdnn/dd sort polldate // sorting will make a nicer line graph, eventually rename margin spread generate margin = ernst - braley // One layer, a scatter plot scatter margin polldate scatter margin polldate if polldate > td(1sep2014) // Calculate trailing averages against which // polls are compared // (1) Compare to polls in the last 21 days // (2) but only if there are at least 3 polls in that time frame generate trailing21 = . forvalues i = 1/`=_N' { local j = `i' - 1 generate win`i' = (polldate - polldate[`i']) >= -21 /// & (polldate - polldate[`i']) <= 0 egen trailing21`i' = total(margin) if win`i'==1 egen pool`i' = total(win`i') generate trailmargin`i' = trailing21`i'/pool`i' replace trailing21 = trailmargin`i' in `i' if pool`i' >= 3 drop win`i' trailing21`i' pool`i' trailmargin`i' } // lag the trailing value generate trailing = trailing21[_n-1] // At this point we can throw away data we don't // need for graphing keep if polldate > td(1sep2014) // Second layer, a line plot line trailing polldate // Both layers combined scatter margin polldate || line trailing polldate // Third layer, a range area plot generate lmoe = trailing - 3.5 generate umoe = trailing + 3.5 twoway rarea umoe lmoe polldate // relation between range and line twoway rarea umoe lmoe polldate || line trailing polldate // All three layers combined twoway rarea umoe lmoe polldate || /// scatter margin polldate || /// line trailing polldate // Adding and modifying aesthetics label variable polldate "Poll ending date" label variable margin "Polling margin" label variable trailing "Trailing average" label variable lmoe "-3.5%" label variable umoe "+3.5%" twoway rarea umoe lmoe polldate, color(gs12) || /// scatter margin polldate, color(black) || /// line trailing polldate, color(red) /// yline(8.5, lpattern(dot)) yscale(range(-10(5)12)) /// ylabel(-10 "+10% Braley" -5 "+5% Braley" 0 "0" 5 "+5% Ernst" 10 "+10% Ernst", angle(0)) /// title("Iowa Senate Polls Converged") subtitle("(But Badly Missed the Outcome)") /// note("data from Huffington Post, analysis after fivethirtyeight.com")