asjadnaqvi / stata-spider

spider: A Stata package for spider plots.
MIT License
12 stars 1 forks source link
ado package plot spider stata

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

v1.5 (13 Oct 2024)

v1.4 (04 Oct 2024)

v1.33 (02 Jul 2024)

v1.32 (11 Jun 2024)

v1.31 (11 May 2024)

v1.3 (16 Feb 2024)

v1.23 (12 Nov 2023)

v1.22 (07 Jul 2023)

v1.21 (10 Jun 2023)

v1.2 (20 May 2023)

v1.1 (24 Dec 2022)

v1.0 (13 Oct 2022)