grid paper using Stata

I just wrote a program to make grids using Stata! Not much use for many people, but if you have a friend who is a knitter and needs a grid template, well this may come in handy 🙂

This is an ado file, so just: 1) copy the text below the image into a text editor; 2) save it with the extension “.ado”; 3) put it in your Stata personal directory

The following grid was generated using the command:
mkgrid 60 80 "44 162 95" vthin label alt
grid_60x80


/**
Program to produce a y by x grid
Carl Higgs 16 April 2016

Input parameters (arguments):
y number of vertical spaces
x number of horizontal spaces
colour line colour
width line width
labels whether numbered axes are desired (if so, write "label" here, or leave blank)
alt if you have specified having labels, you might want them to alternate: type "alt"

e.g.
mkgrid 60 80
mkgrid 60 80 black
mkgrid 60 80 black vthin label
mkgrid 60 80 black vthin label alt
**/
* capture program drop mkgrid
program mkgrid, rclass
version 13.1
args y x colour width labels alt
di "y is `y' and x is `x'"
drop _all
loc obs = max(`y',`x')
set obs `obs'
if `y'==`obs' {
egen y = fill(1(1)`y')
gen x = `x'
}
else {
gen y = `y'
egen x = fill(1(1)`x')
}

loc aspect = `y'/`x'

if "`labels'"=="label" {
tw sc y x, ms(i) ///
graphregion(fcolor(white) lcolor(white)) scheme(s2color) ///
xlab(0(1)`x',grid nogextend gmin glc("`colour'") `alt' labsize(vsmall) glwidth("`width'")) ///
ylab(0(1)`y',grid nogextend gmin glc("`colour'") `alt' angle(h) labsize(vsmall) glwidth("`width'")) ///
ytitle("") ///
xtitle("") ///
aspect(`aspect') ///
xsca(noextend) ysca(noextend) ///
name(grid_`y'x`x',replace)
}

else {
tw sc y x, ms(i) ///
graphregion(fcolor(white) lcolor(white)) scheme(s2color) ///
xlab(0(1)`x',grid nogextend gmin glc("`colour'") glwidth("`width'")) ///
ylab(0(1)`y',grid nogextend gmin glc("`colour'") glwidth("`width'")) ///
ytitle("") ///
xtitle("") ///
aspect(`aspect') ///
xsca(off) ysca(off) ///
name(grid_`y'x`x',replace)
}

end

A 5×5 grid:
grid_5x5

A 2496×738 grid, towards the limits of what this code can do, the artifacts of electrical glitch spirits start to surface:
grid_2496x738