Sort categories in bar chart by statistic

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Sort categories in bar chart by statistic

ChrisKeran
I'm trying to create GPL that will sort categories in dim(3) by the percent
statistic in dim(2).


* Chart Builder.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=PrimarySubspecialty
COUNT()[name="COUNT"]
    ContinuumSubscriber MISSING=LISTWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  SOURCE: s=userSource(id("graphdataset"))
  DATA: PrimarySubspecialty=col(source(s), name("PrimarySubspecialty"),
unit.category())
  DATA: COUNT=col(source(s), name("COUNT"))
  DATA: ContinuumSubscriber=col(source(s), name("ContinuumSubscriber"),
unit.category())
  COORD: rect(dim(1,2), cluster(3,0))
  GUIDE: axis(dim(3), label("Primary Subspecialty"))
  GUIDE: axis(dim(2), label("Percent"))
  GUIDE: legend(aesthetic(aesthetic.color.interior),
label("ContinuumSubscriber"))
  GUIDE: text.title(label("Percent of Primary Subspecialty by
ContinuumSubscriber"))
*  SCALE: cat(dim(3),
sort.statistic(summary.percent.count(ContinuumSubscriber)))*
  SCALE: linear(dim(2), min(0), max(100), include(0))
  SCALE: cat(aesthetic(aesthetic.color.interior), include("0.00", "1.00"))
  SCALE: cat(dim(1), include("0.00", "1.00"))
  ELEMENT:
interval(position(summary.percent(ContinuumSubscriber*COUNT*PrimarySubspecialty,
    base.coordinate(dim(3)))), color.interior(ContinuumSubscriber),
shape.interior(shape.square))
END GPL.

This syntax produces this graph, however, I would like the categories along
the X axis to be sorted by %Yes...
<http://spssx-discussion.1045642.n5.nabble.com/file/t340635/Graph.jpg>




--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Sort categories in bar chart by statistic

bdates
Chris,

Peder Rogman sent this through for me a couple of years ago. It sorts by frequency in treatment group.  You might be able to adjust if for percents. The data is raw data with frequency of each age per group. Notice that it uses varstocases.

DATA LIST LIST /
Age  Treatment  Control (3F8).
BEGIN DATA
23 1 0
24 0 1
25 6 3
26 7 10
27 17 10
28 33 30
29 40 28
30 44 32
31 34 52
32 25 25
33 14 18
34 7 14
35 1 3
36 0 2
37 0 1
END DATA.

DATASET NAME BD.

AGGREGATE
  /OUTFILE       = * MODE=ADDVARIABLES OVERWRITEVARS=YES
  /BREAK         =
  /Treatment_sum = SUM(Treatment)
  /Control_sum   = SUM(Control)
.
COMPUTE   Treatment_frc = Treatment/Treatment_sum.
COMPUTE   Control_frc   = Control/Control_sum.
*Set decimals in graph.
FORMATS Treatment_frc Control_frc  (F8.3).

SORT CASES Treatment (D) Age (A).

VARSTOCASES
  /MAKE data FROM Treatment_frc Control_frc
  /INDEX=Group(2)
  /KEEP=Age
  /NULL=KEEP.

VALUE LABELS  Group  1'Treatment'  2'Control'.

GGRAPH
  /GRAPHDATASET
   NAME          = "graphdataset"
   VARIABLES     = Age [LEVEL=NOMINAL]
                   data
                   Group
   MISSING       = LISTWISE
   REPORTMISSING = NO
  /GRAPHSPEC
   SOURCE        = INLINE.
BEGIN GPL
  SOURCE:  s    =userSource(id("graphdataset"))
  DATA:    Age  =col(source(s), name("Age"),   unit.category())
  DATA:    data =col(source(s), name("data"))
  DATA:    Group=col(source(s), name("Group"), unit.category())
  COORD:   rect(dim(1,2), cluster(3,0))
  GUIDE:   axis(dim(3), label("Age"))
  GUIDE:   legend(aesthetic(aesthetic.color.interior), label("Group"))
  SCALE:   cat(aesthetic(aesthetic.color.interior), include("1", "2"))
  SCALE:   cat(dim(1), include("1", "2"))
  SCALE:   linear(dim(2), include(0))
COMMENT: The following SCALE statement is generated as sort.values() and
generates an error. change to     sort data() to keep original order
  SCALE:   cat(dim(3), sort.data())
  ELEMENT: interval(position(Group*data*Age), color.interior(Group),
shape.interior(shape.square))
END GPL.

Brian

From: SPSSX(r) Discussion <[hidden email]> on behalf of ChrisKeran <[hidden email]>
Sent: Monday, March 30, 2020 11:06 AM
To: [hidden email] <[hidden email]>
Subject: Sort categories in bar chart by statistic
 
I'm trying to create GPL that will sort categories in dim(3) by the percent
statistic in dim(2).


* Chart Builder.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=PrimarySubspecialty
COUNT()[name="COUNT"]
    ContinuumSubscriber MISSING=LISTWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  SOURCE: s=userSource(id("graphdataset"))
  DATA: PrimarySubspecialty=col(source(s), name("PrimarySubspecialty"),
unit.category())
  DATA: COUNT=col(source(s), name("COUNT"))
  DATA: ContinuumSubscriber=col(source(s), name("ContinuumSubscriber"),
unit.category())
  COORD: rect(dim(1,2), cluster(3,0))
  GUIDE: axis(dim(3), label("Primary Subspecialty"))
  GUIDE: axis(dim(2), label("Percent"))
  GUIDE: legend(aesthetic(aesthetic.color.interior),
label("ContinuumSubscriber"))
  GUIDE: text.title(label("Percent of Primary Subspecialty by
ContinuumSubscriber"))
*  SCALE: cat(dim(3),
sort.statistic(summary.percent.count(ContinuumSubscriber)))*
  SCALE: linear(dim(2), min(0), max(100), include(0))
  SCALE: cat(aesthetic(aesthetic.color.interior), include("0.00", "1.00"))
  SCALE: cat(dim(1), include("0.00", "1.00"))
  ELEMENT:
interval(position(summary.percent(ContinuumSubscriber*COUNT*PrimarySubspecialty,
    base.coordinate(dim(3)))), color.interior(ContinuumSubscriber),
shape.interior(shape.square))
END GPL.

This syntax produces this graph, however, I would like the categories along
the X axis to be sorted by %Yes...
<https://nam12.safelinks.protection.outlook.com/?url=http%3A%2F%2Fspssx-discussion.1045642.n5.nabble.com%2Ffile%2Ft340635%2FGraph.jpg&amp;data=02%7C01%7Cbdates%40SWSOL.ORG%7C471efdb09a4b4f8dcb5408d7d4bbde53%7Cecdd61640dbd4227b0986de8e52525ca%7C0%7C0%7C637211775661124923&amp;sdata=RYaqu%2ByT1rxijgCSQlYKFoPTyZN%2BHj4BovfdsZ%2FZcD4%3D&amp;reserved=0>




--
Sent from: https://nam12.safelinks.protection.outlook.com/?url=http%3A%2F%2Fspssx-discussion.1045642.n5.nabble.com%2F&amp;data=02%7C01%7Cbdates%40SWSOL.ORG%7C471efdb09a4b4f8dcb5408d7d4bbde53%7Cecdd61640dbd4227b0986de8e52525ca%7C0%7C0%7C637211775661124923&amp;sdata=QyZk9okNdGMzmavv1IfkLl1YEMAN%2FBPdV6OqFMFyEVA%3D&amp;reserved=0

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Sort categories in bar chart by statistic

ChrisKeran
In reply to this post by ChrisKeran
Thank you, Brian. I guess I was hoping to create this graph without
reorganizing my data, but I will do so if necessary. Do others have
solutions without reorganizing the data?

Thanks again,
Chris



--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD