Skip to content

asjadnaqvi/stata-spider

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StataMin issues license Stars version release

Installation | Syntax | Citation guidelines | Examples | Feedback | Change log


spider-1

spider v1.51

(09 Nov 2024)

This package provides the ability to draw spiders Stata. It is based on the Spider plots guide on Medium

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.

The SSC version (v1.5):

ssc install spider, replace

Or it can be installed from GitHub (v1.51):

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

The palettes package is required to run this command:

ssc install palettes, replace
ssc install colrspace, replace
ssc install graphfunctions, replace

Please note that graphfunctions is a new program with features rolling out regularly. In case the SSC version is not working, please update it directly from the graphfunctions repository. See the link for instructions.

Even if you have the package installed, make sure that it is updated ado update, update.

If you want to make a clean figure, then it is advisable to load a clean scheme. These are several available and I personally use the following:

ssc install schemepack, replace
set scheme white_tableau  

You can also push the scheme directly into the graph using the scheme(schemename) option. See the help file for details or the example below.

I also prefer narrow fonts in figures with long labels. You can change this as follows:

graph set window fontface "Arial Narrow"

Syntax

The syntax for the latest version is as follows:

spider var [if] [in] [weight], 
                [ by(var) over(var) alpha(num 0-100) rotate(num) smooth(num 0-1) palette(str)
                  range(numlist) cuts(num) lwidth(str) lpattern(list) msymbol(list) rotatelabel
                  format(fmt) wrap(num) msize(str) mlwidth(str) displacelab(num) displacespike(num) grid  
                  gcolor(str) gwidth(str) gpattern(str) glabsize(str) glabcolor(str) glabangle(str) glabopsition(str) 
                  rline(numlist) rlinecolor(str) rlinewidth(str) rlinepattern(str)
                  scolor(str) swidth(str) slabsize(str) slabcolor(str)
                  nolegend legpositon(num) legpositon(num) legcolumns(num) legsize(num) xsize(num) ysize(num)
                  stat(mean|sum) pad(num) * ]

See the help file help spider for details.

Basic syntax 1: Wide form

spider variables, over(var)

Basic syntax 2: Long form

spider variable, by(var) over(var)

See help file for details.

Citation guidelines

Software packages take countless hours of programming, testing, and bug fixing. If you use this package, then a citation would be highly appreciated. Suggested citations:

in BibTeX

@software{spider,
   author = {Naqvi, Asjad},
   title = {Stata package ``spider''},
   url = {https://github.com/asjadnaqvi/stata-spider},
   version = {1.5},
   date = {2024-10-13}
}

or simple text

Naqvi, A. (2024). Stata package "spider" version 1.5. Release date 13 October 2024. https://github.com/asjadnaqvi/stata-spider.

or see SSC citation (updated once a new version is submitted)

Examples

Set up the data:

clear

use "https://github.com/asjadnaqvi/stata-spider/blob/main/data/spider_data2.dta?raw=true", clear

and test the command:

spider index, by(policy) 

spider index, by(policy) over(region)

spider index, by(policy) over(region) alpha(0)

spider index, by(policy) over(region) alpha(0) msym(none)

spider index, by(policy) over(region) alpha(0) msym(square) msize(0.2) ra(0(20)100)

spider index, by(policy) over(region) alpha(0) rot(30)

spider index, by(policy) over(region) alpha(0) rot(30) rotatelab

Smooth the spiders

spider index if inlist(region,1,6), by(policy) over(region) ra(10(10)80) alpha(2) rot(30) format(%6.0f)

spider index if inlist(region,1,6), by(policy) over(region) ra(10(10)80) alpha(2) rot(30) format(%6.0f) smooth(0)

spider index if inlist(region,1,6), by(policy) over(region) ra(10(10)80) alpha(2) rot(30) format(%6.0f) smooth(0.5)

spider index if inlist(region,1,6), by(policy) over(region) ra(10(10)80) alpha(2) rot(30) format(%6.0f) smooth(1)

Palettes

spider index, by(policy) over(region) smooth(0.1) palette(tol vibrant) lw(0.4) msym(none) alpha(2) rot(30) format(%6.0f)

spider index, by(policy) over(region) smooth(0.1) palette(carto Bold) lw(0.4) msym(none) alpha(2) rot(30) format(%6.0f)

Customize grids and spikes

spider index, by(policy) over(region) ra(10(10)80) smooth(0.1) gc(eltblue) gw(0.05) rot(30) format(%6.0f)

spider index, by(policy) over(region) ra(10(10)80) smooth(0.1) gc(eltblue) gw(0.05) sc(black) sw(0.1) rot(30) format(%6.0f)

spider index, by(policy) over(region) ra(10(10)80) smooth(0.1) gc(eltblue) gw(0.05) sc(eltblue) sw(0.3) displacelab(20) displacespike(10) rotatelab rot(30) format(%6.0f)

Legends and custom dimensions (v1.2)

spider index, by(policy) over(region) smooth(0) alpha(5) rot(30) format(%6.0f) xsize(4) ysize(3) 

spider index, by(policy) over(region) smooth(0) alpha(5) rot(30) format(%6.0f) xsize(4) ysize(3) legpos(3) legcol(1)

Additional features from v1.5:

spider index if inlist(region,1,2), by(policy) over(region) smooth(0) alpha(10) rot(15) format(%6.0f) xsize(4) ysize(3) legpos(3) legcol(1) wrap(6) grid range(0 10 20 40 80) rline(35) msym(circle square) msize(0.8)

Try a different scaling

gen index2 = index / 100

spider index2, by(policy) over(region) ra(0.1(0.1)0.8) format(%5.1f) rot(30)  smooth(0) alpha(0) glabs(2)

Range label options (v1.21)

spider index2, by(policy) over(region) ra(0.1(0.1)0.8) format(%5.1f)  rot(30) smooth(0) alpha(0) glabc(blue) glabs(1.5) glaba(-90)

v1.4 options

Word wrapping

spider index, by(policy) over(region) smooth(0) alpha(5) wrap(5) rot(30)

Let's make our data wide:

encode policy, gen(policy2)

summ policy2, meanonly
local items = r(max)

forval i = 1 /`items' {
	local val`i' : label policy2 `i' 
}



collapse (mean) index, by(policy2 region)


reshape wide index, i(region) j(policy2) 

forval i = 1 / `items' {
	lab var index`i' "`val`i''"
}


spider index*, over(region) smooth(0) alpha(5) wrap(5)  rot(30)

v1.5 options: grid + lists + reference lines.

Reload the data:

use "https://github.com/asjadnaqvi/stata-spider/blob/main/data/spider_data2.dta?raw=true", clear
spider index, by(policy) over(region) alpha(0) wrap(8) ra(0(20)80) format(%5.0f) rot(30)

spider index, by(policy) over(region) alpha(0) wrap(8) ra(0 10 30 50 70 80) format(%5.0f) rot(30)

spider index, by(policy) over(region) alpha(0) wrap(8) ra(0(20)80) format(%5.0f) grid rot(30)

spider index, by(policy) over(region) alpha(0) wrap(8) ra(0(20)80) format(%5.0f) grid rline(25 50 70) rlinew(0.2) rlinec(black) rot(30)

spider index, by(policy) over(region) alpha(0) wrap(8) ra(0(20)80) cuts(5) format(%5.0f) ///
	msym(S T O +) msize(1.2) rot(30)

spider index, by(policy) over(region) alpha(0) wrap(8) ra(0(20)80) cuts(5) format(%5.0f) ///
	lp(dash dot solid -- -.-) rot(30)

Bonus: Valentines day spending spider graph

Here is a proper example of data in wide form, which in previous versions (<v1.4) would have required reshaping the data:

set scheme white_tableau
graph set window fontface "Abel"

import delim using "https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-02-13/gifts_gender.csv", clear

lab var spendingcelebrating "Spending celebrating"
lab var greetingcards "Greeting cards"
lab var eveningout "Evening out"
lab var giftcards "Gift cards"

spider spendingcelebrating- giftcards, over(gender) smooth(0.2) alpha(15) lw(0.4) palette(w3 default, select(1 4)) rotatelab ///
	legcol(3) legsize(3) msize(0.15) sc(black) ccolor(gs13) range(0 60) cuts(7) format(%5.0f) displacelab(15) slabsize(2) wrap(5) ///
	title("{fontface Merriweather Bold:♥ Candy crush ♥}", size(7) color(cranberry))  ///
	subtitle("(Percentage positive respones by spending category)", size(2)) ///
	note("Source: TidyTuesday, 14th Feb 2024.", size(1.5)) ///
	plotregion(margin(t-5 b-5 l-20 r-20))

Here is another version which showcases v1.5 updates:

spider spendingcelebrating- giftcards, over(gender) smooth(0.2) alpha(15) lw(0.4) palette(w3 default, select(1 4)) rotatelab  rline(25 50)  ///
	legcol(3) legsize(3) msize(0.15) sc(black) gcolor(gs13)  format(%5.0f) displacelab(20) glabsize(2) wrap(5) ///
	plotregion(margin(l-20 r-20)) ra(0(10)60) grid lp(solid dash) ///
	title("{fontface Merriweather Bold:Valentines day spending by gender}", size(5) color(cranberry))  ///
	note("Source: TidyTuesday, 14th Feb 2024.", size(1.5)) 

Feedback

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

Change log

v1.51 (09 Nov 2024)

A minor release with some major changes

  • Starting point is now the 12 o' clock (North) position. This has been requested countless times. One can still rotate by 90 degrees to start from the old (Stata) default 3 o' clock (East) position.
  • Draw order is now clockwise. Again requested a lot. Users can change the direction to counter-clockwise by using the new option flip.
  • Labels are now on the starting north-facing line.
  • First category now correctly shows on the first position. Previously it was on the second position.
  • Fix a bug where non-integer values under certain conditions were causing the program to crash.
  • Minor Code cleanups.

v1.5 (13 Oct 2024)

  • Now requires dependency graphfunctions (check versions and update to the latest!).
  • Options msymbol() and lwidth() now take lists. If there are less elements than the number of lines, then the last values are inherited by the remaining lines.
  • Added option grid which draws straight lines as grids rather than circles.
  • Added options rline(), rlinec(), rlinep(), rlinew() for reference lines. Can take lists.
  • c*() options changes to g*() options to reflect grid line values. E.g. ccolor() changed to gcolor().
  • ra*() options renamed to g*() options to reflect grid label values. E.g. ralabelsize() changed to glabelsize().
  • Option range() now accepts numerical lists (numlist). This makes cuts() almost redundant but it can still be used if no custom range is specified and one needs to increase the number of cuts for auto-calculated ranges.
  • Option gwidth() added.
  • Several updates to defaults.
  • Minor code cleanups.

v1.4 (04 Oct 2024)

  • Weights are now allowed.
  • Users can now specify variable lists (wide form) in addition to long form data. See help file.
  • Added pad() to control minmax displacement of the axis range
  • Added wrap() to allow text wrapping. Requires the graphfunctions package.
  • Added n() to control how many points are generated to plot the splines between two points.
  • Added stat() where users can choose between stat(mean) (default) or stat(sum) if the data is collapsed.
  • Code cleanup.

v1.33 (02 Jul 2024)

  • Fixed a minor error where numeric over() and by() were not being labeled correctly.

v1.32 (11 Jun 2024)

  • Added wrap() to wrap labels.

v1.31 (11 May 2024)

  • changed raformat() to just format() to standardize the use across packages.
  • format() default improved to show decimals.
  • Checks added for by() and over() to have minimum number of accepted categories.
  • Stata default passthru options improved.

v1.3 (16 Feb 2024)

  • Complete rework of the package to take in the data in the long form. While this might be convinient for users who were using it for wide data, the newer version handle the data and labels better and is faster. The wide option might still be added back in later. Please note that change in the by() and over() options.
  • rotatelabel option added.
  • Better legend controls. Please make sure value labels are properly defined that are passed on to legends.

v1.23 (12 Nov 2023)

  • Added support for slabcolor() (requested by Christian Gessinger).
  • Added saving().

v1.22 (07 Jul 2023)

  • Fixed a bug where labeled over() categories were not passing correctly (reported by Kamala Kaghoma).

v1.21 (10 Jun 2023)

  • Two options added for range labels: ralabcolor(), ralabangle().

v1.2 (20 May 2023)

  • Several legend options added (requested by Marc Kaulisch). These include nolegend, legendpos(), legendsize(), legendcol().
  • xsize() and ysize() added to allow users to control the dimensions.
  • Minor code cleanups.
  • Help file cleanup.

v1.1 (24 Dec 2022)

  • Fixed some bugs.
  • Better checks for merges.

v1.0 (13 Oct 2022)

  • First release