Skip to content

Latest commit

 

History

History
589 lines (394 loc) · 15.2 KB

README.md

File metadata and controls

589 lines (394 loc) · 15.2 KB

StataMin issues license Stars version release

Installation | Examples | Feedback | Change log


graphfunctions v1.5

(05 Nov 2024)

A suite of graph functions for Stata. The program is designed to be called by other programs, but it can be used as a standalone as well. The page will provide some minimum examples, but for the full scope, see the relevant help files.

Currently, this package contains:

Program Version Updated Description
labsplit 1.1 08 Oct 2024 Text wrapping
catspline 1.0 04 Oct 2024 Catmull-Rom splines
arc 1.1 11 Oct 2024 Draw arcs between two points
shapes 1.3 05 Nov 2024 Contains shapes circle, shapes pie, shapes square, shapes rotate, shapes area

The programs here are designed/upgraded/bug-fixed on a needs basis, mostly to support other packages. If you have specific requests, or find major bugs, then please open an issue.

Installation

The package can be installed via SSC or GitHub. The GitHub version, might be more recent due to bug fixes, feature updates etc, and may contain syntax improvements and changes in default values. See version numbers below. Eventually the GitHub version is published on SSC.

SSC (v1.4):

ssc install graphfunctions, replace

GitHub (v1.5):

net install graphfunctions, from("https://raw.githubusercontent.com/asjadnaqvi/stata-graphfunctions/main/installation/") replace

See the help file help graphfunctions for details.

If you want to make a clean figure, then it is advisable to load a clean scheme, especially if you are not using newer Stata versions. My own setting is the following:

ssc install schemepack, replace
set scheme white_tableau  
graph set window fontface "Arial Narrow"

Examples

labsplit

(v1.1: 08 Oct 2024)

The program allows users to split text labels based on flexible or fixed character length or word positions.

Syntax:

labsplit variable, [ wrap(int) word(int) strict generate(newvar) ]

Examples:

clear
set obs 5
gen x = 1
gen y = _n

gen mylab = ""

replace mylab = "Test this really-hyphenated label." in 1
replace mylab = "Yet another label to test." in 2
replace mylab = "This is the third label" in 3
replace mylab = "How about we test this label as well" in 4
replace mylab = "Finally we are at the fifth label" in 5

Let's test the labsplit command:

labsplit mylab, wrap(10) gen(newlab1)
labsplit mylab, wrap(10) gen(newlab2) strict
labsplit mylab, word(2) gen(newlab3)

Code for figures:

twoway (scatter y x, mlabel(mylab)   mlabsize(3)), title("Standard")
twoway (scatter y x, mlabel(newlab1) mlabsize(3)), title("Wrapping")

twoway (scatter y x, mlabel(newlab2) mlabsize(3)), title("Wrapping strict") 
twoway (scatter y x, mlabel(newlab3) mlabsize(3)), title("Word wrap")

catspline

(v1.0: 04 Oct 2024)

The program allows users to generate splines based on the Catmull-Rom algorithm.

Syntax:

catspline y x, [ rho(num [0,1]) obs(int) close genx(newvar) geny(newvar) ]

Examples:

clear
set obs 5
set seed 2021

gen x = runiformint(1,5)
gen y = runiformint(1,5)


catspline y x

twoway ///
	(scatter y x) ///
	(line _y _x,  cmissing(n))

cap drop _id _x _y
catspline y x, close 

twoway ///
	(scatter y x) ///
	(line _y _x,  cmissing(n))

arc

(v1.1: 11 Oct 2024)

Draw minor or major arcs between two points. The arc orientation and be switched using swap, and major arcs can be drawn using major.

Syntax:

arc, x1(num) y1(num) x2(num) y2(num) [ radius(num) n(int) swap major genx(newvar) geny(newvar) replace ]

Examples:

arc, y1(-2) x1(-4) y2(4) x2(2) rad(6) replace

twoway ///
	(scatteri -2 -4)  (scatteri 4 2) ///
	(scatteri `r(ycirc)' `r(xcirc)') ///
	(line _y _x)  ///
	, legend(order(1 "Point 1" 2 "Point 2" 3 "Circumcenter" 4 "Arc") pos(6) row(1)) ///
	xlabel(-10(2)10) ylabel(-10(2)10) aspect(1) xsize(1) ysize(1) ///
	title("Right of starting point - minor")

arc, y1(-2) x1(-4) y2(4) x2(2) rad(6) major replace		

twoway ///
	(scatteri -2 -4)  (scatteri 4 2) ///
	(scatteri `r(ycirc)' `r(xcirc)') ///
	(line _y _x)  ///
	, legend(order(1 "Point 1" 2 "Point 2" 3 "Circumcenter" 4 "Arc") pos(6) row(1)) ///
	xlabel(-10(2)10) ylabel(-10(2)10) aspect(1) xsize(1) ysize(1) ///
	title("Right of starting point - major")	
	

arc, y1(-2) x1(-4) y2(4) x2(2) rad(6) swap replace		

twoway ///
	(scatteri -2 -4)  (scatteri 4 2) ///
	(scatteri `r(ycirc)' `r(xcirc)') ///
	(line _y _x)  ///
	, legend(order(1 "Point 1" 2 "Point 2" 3 "Circumcenter" 4 "Arc") pos(6) row(1)) ///
	xlabel(-10(2)10) ylabel(-10(2)10) aspect(1) xsize(1) ysize(1) ///
	title("Left of starting point - minor")

arc, y1(-2) x1(-4) y2(4) x2(2) rad(6) swap major replace

twoway ///
	(scatteri -2 -4)  (scatteri 4 2) ///
	(scatteri `r(ycirc)' `r(xcirc)') ///
	(line _y _x)  ///
	, legend(order(1 "Point 1" 2 "Point 2" 3 "Circumcenter" 4 "Arc") pos(6) row(1)) ///
	xlabel(-10(2)10) ylabel(-10(2)10) aspect(1) xsize(1) ysize(1) ///
	title("Left of starting point - Major")

shapes

(v1.3: 15 Oct 2024)

Circles

Syntax:

shape circle, [ n(int) rotate(degrees) radius(num) x0(num) y0(num) genx(newvar) geny(newvar) genorder(newvar) genid(newvar) replace stack/append ]

Examples:

shapes circle, replace

twoway /// 
		(connected _y _x, mlabel(_order)) ///
		, xsize(1) ysize(1) aspect(1)	///
		xlabel(-10 10) ylabel(-10 10)

shapes circle, n(6) replace


twoway /// 
		(connected _y _x, mlabel(_order)) ///
		, xsize(1) ysize(1) aspect(1)	///
		xlabel(-10 10) ylabel(-10 10)

shapes circle, n(6) rotate(30) replace

twoway /// 
		(connected _y _x, mlabel(_order)) ///
		, xsize(1) ysize(1) aspect(1)	///
		xlabel(-10 10) ylabel(-10 10)

shapes circle, rotate(45) rad(8) n(4) replace		

twoway /// 
		(connected _y _x, mlabel(_order)) ///
		, xsize(1) ysize(1) aspect(1)	///
		xlabel(-10 10) ylabel(-10 10)

shapes circle, n(100) replace	

twoway /// 
		(line _y _x) ///
		, xsize(1) ysize(1) aspect(1)	///
		xlabel(-10 10) ylabel(-10 10)

shapes circle,    	      n(8) replace
shapes circle, rotate(30) n(6) rad(8) append 
shapes circle, rotate(60) n(4) rad(3)  x0(1) y0(1) append

twoway (connected _y _x, cmissing(n)), aspect(1)

Pies

Syntax:

shape pie, [ n(int) start(degrees) end(degrees) rotate(degrees) radius(num) x0(num) y0(num) genx(newvar) geny(newvar) genorder(newvar) genid(newvar) dropbase replace stack/append ]

Examples:

shapes pie, end(60) replace

twoway (area _y _x), xlabel(-10 10) ylabel(-10 10) xsize(1) ysize(1) aspect(1)

shapes pie, start(0) end(270) n(200) replace

twoway (area _y _x), xlabel(-10 10) ylabel(-10 10) xsize(1) ysize(1) aspect(1)

clear
shapes pie, start(0) end(45) ro(0)		rad(5) 	replace
shapes pie, start(0) end(45) ro(30) 	rad(6)  stack
shapes pie, start(0) end(45) ro(60) 	rad(7)  stack
shapes pie, start(0) end(45) ro(90) 	rad(8)  stack
shapes pie, start(0) end(45) ro(120) 	rad(9)  stack
shapes pie, start(0) end(45) ro(150) 	rad(10) stack


twoway (area _y _x, fcolor(%90) cmissing(n) nodropbase)	///
	, xlabel(-10 10) ylabel(-10 10) xsize(1) ysize(1) aspect(1)

without base:

shapes pie, start(0) end(230) rad(10) rotate(90) n(200) dropbase replace
shapes pie, start(0) end(230) rad(9)  rotate(90) n(200) dropbase append
shapes pie, start(0) end(200) rad(8)  rotate(90) n(200) dropbase append
shapes pie, start(0) end(180) rad(7)  rotate(90) n(200) dropbase append
shapes pie, start(0) end(160) rad(6)  rotate(90) n(200) dropbase append

twoway (line _y _x, cmissing(n) nodropbase)	///
	, xlabel(-10 10) ylabel(-10 10) xsize(1) ysize(1) aspect(1)

without base flipped direction:

shapes pie, start(0) end(350) rad(10) rotate(90) n(200) dropbase flip replace
shapes pie, start(0) end(150) rad(9)  rotate(90) n(200) dropbase flip append
shapes pie, start(0) end(130) rad(8)  rotate(90) n(200) dropbase flip append
shapes pie, start(0) end(80) rad(7)  rotate(90) n(200) dropbase flip append
shapes pie, start(0) end(60) rad(6)  rotate(90) n(200) dropbase flip append

twoway (line _y _x, cmissing(n) nodropbase)	///
	, xlabel(-10 10) ylabel(-10 10) xsize(1) ysize(1) aspect(1)

Square

shapes square, len(8) rotate(90) replace

twoway ///
	(area _y _x, nodropbase fcolor(%50))	///
	(scatter _y _x, mlab(_order))	///
		, ///
		legend(off) ///
		xlabel(-10(2)10) ylabel(-10(2)10) xsize(1) ysize(1) aspect(1)

shapes square, x0(1) y0(1) len(10) rotate(40) replace
shapes square, x0(1) y0(1) len(9) rotate(30) append
shapes square, x0(1) y0(1) len(8) rotate(20) append
shapes square, x0(1) y0(1) len(7) rotate(10) append
shapes square, x0(1) y0(1) len(6) rotate(0)  append		


twoway ///
	(area _y _x, cmissing(n) nodropbase fcolor(%100) lw(0.1) lc(white))	///
		, ///
		legend(off) ///
		xlabel(-10(2)10) ylabel(-10(2)10) xsize(1) ysize(1) aspect(1)

Rotate

Let's generate a basic shape:

shapes square, x0( 5) y0(0) len(5) replace
shapes square, x0(-5) y0(0) len(5) append

twoway ///
	(area _y _x, cmissing(n) nodropbase fcolor(%80) lw(0.1) lc(white))	///
	(scatteri 0 0) ///
		, ///
		legend(off) ///
		xlabel(-10(2)10) ylabel(-10(2)10) xsize(1) ysize(1) aspect(1)	

shapes rotate _y _x, rotate(30)


twoway ///
	(area _y _x, cmissing(n) nodropbase fcolor(%80) lw(0.1) lc(white))	///
	(area ynew  xnew, cmissing(n) nodropbase fcolor(%80) lw(0.1) lc(white))	///
	(scatteri 0 0) ///
		, ///
		legend(off) ///
		xlabel(-10(2)10) ylabel(-10(2)10) xsize(1) ysize(1) aspect(1)	

shapes rotate _y _x, rotate(30) x0(5) y0(5) genx(xnew) geny(ynew)

twoway ///
	(area _y _x, cmissing(n) nodropbase fcolor(%80) lw(0.1) lc(white))	///
	(area ynew  xnew, cmissing(n) nodropbase fcolor(%80) lw(0.1) lc(white))	///
	(scatteri 0 0) ///
		, ///
		legend(off) ///
		xlabel(-10(2)10) ylabel(-10(2)10) xsize(1) ysize(1) aspect(1)	

shapes rotate _y _x, rotate(60) center genx(xnew2) geny(ynew2)		

twoway ///
	(area _y _x, cmissing(n) nodropbase fcolor(%80) lw(0.1) lc(white))	///
	(area ynew2  xnew2, cmissing(n) nodropbase fcolor(%80) lw(0.1) lc(white))	///
	(scatteri 0 0) ///
		, ///
		legend(off) ///
		xlabel(-10(2)10) ylabel(-10(2)10) xsize(1) ysize(1) aspect(1)	

shapes rotate _y _x, rotate(30) center by(_id) genx(xnew3) geny(ynew3)			
	
twoway ///
	(area _y _x, cmissing(n) nodropbase fcolor(%80) lw(0.1) lc(white))	///
	(area ynew3  xnew3, cmissing(n) nodropbase fcolor(%80) lw(0.1) lc(white))	///
	(scatteri 0 0) ///
		, ///
		legend(off) ///
		xlabel(-10(2)10) ylabel(-10(2)10) xsize(1) ysize(1) aspect(1)

Area

clear
shapes pie, start(0) end(45) ro(0)		rad(5) 	replace
shapes pie, start(0) end(45) ro(30) 	rad(6)  stack
shapes pie, start(0) end(45) ro(60) 	rad(7)  stack
shapes pie, start(0) end(45) ro(90) 	rad(8)  stack
shapes pie, start(0) end(45) ro(120) 	rad(9)  stack
shapes pie, start(0) end(45) ro(150) 	rad(10) stack


twoway (area _y _x, fcolor(%90) cmissing(n) nodropbase)	///
	, xlabel(-10 10) ylabel(-10 10) xsize(1) ysize(1) aspect(1)

calculate the areas

shapes area _y _x, by(_id) 

Generate some ranking of areas and plot

xtile grps = _area, n(4)	

levelsof _id, local(lvls)

foreach x of local lvls {
	colorpalette reds, nograph
	local myarea `myarea' (area _y _x if _id==`x', fcolor("`r(p`x')'%90") lc("`r(p`x')'") cmissing(n) nodropbase)	
}



twoway ///
	`myarea'	///
	, ///
		xlabel(-10 10) ylabel(-10 10) ///
		legend(off) ///
		xsize(1) ysize(1) aspect(1)

Feedback

Please open an issue to report errors, feature enhancements, and/or other requests.

Change log

v1.5 (05 Nov 2024)

  • shapes square added for squares. Note that shapes circle, n(4) also returns a square but here we define the center-to-corner length using the radius, where as shapes square generates a side with a predefined length. Hence the area of shapes circle, n(4) rad(5) > shapes square, len(5).
  • shapes rotate added for generation rotations at specific points or center of shapes. Note that this is a more advanced rotation than what each individual function provides.
  • shapes area added for calculating the areas (currently in undefined units) using Meister's shoelace formula.
  • Option append added as a substitute for stack. This is more in line with standard Stata syntax.
  • Option flip added to change the drawing direction. This flips from counter-clockwise (Stata default) to clockwise.
  • Better information added for rotations, orientations, and starting points.
  • Cleanup of helpfiles.

v1.4 (15 Oct 2024)

  • shapes is now also mirrored as shape.
  • shape circle updated, and shape pie added.
  • In shapes, users can now also define an origin using x0() and y0() options.
  • More controls and checks.

v1.3 (13 Oct 2024)

  • shapes added. Minor fixes to index tracking.
  • arc bug fixes plus code cleanup.

v1.2 (08 Oct 2024)

  • arc added.
  • Bug fixes in labsplit.
  • Additional checks in programs.

v1.1 (04 Oct 2024)

  • catspline added.

v1.0 (28 Sep 2024)

  • labsplit added.
  • Public release.