Correcting syntax to obtain a stacked bar graph

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

Correcting syntax to obtain a stacked bar graph

bgreen
The syntax below was an attempt to obtain a stacked bar graph. Values for
class (numeric variable) are 1 or 0. Area is a nominal variable of 20
categories.

There are several problems with the output from the graph.


1.      Values on the y-axis are either 1 or 0, rather than a count of all
class 1 and 0 values for each area (e.g area A has 25 class 1 values and
15 class 0 values)
2.      How do I obtain bars that are two-toned in colour, e.g the number of
0’s light blue and the number 1’s dark blue.
3.      How do I add a legend indicating dark blue = class 1 and light blue
class 0 values.
4.      Currently the background colour is pinkish. How can this be set to white?

Syntax used:

GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=AMHS SNFP2 MISSING=LISTWISE
REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  SOURCE: s=userSource(id("graphdataset"))
  DATA: area=col(source(s), name("area"), unit.category())
  DATA: group=col(source(s), name("class"), unit.category())
  GUIDE: axis(dim(1), label("area"))
  GUIDE: axis(dim(2), label("class"))
  ELEMENT: interval(position(area*class), shape.interior(shape.square))
END GPL.



Any assistance with this is much appreciated,

Bob

=====================
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: Correcting syntax to obtain a stacked bar graph

Andy W
Bob,

Please in the future start a new thread, don't reply to other emails (archiving in NABBLE gets messed up). This is pretty easy to do right from the GGRAPH menu, the only thing I had to add was [, map(("0", color.blue), ("1", color.darkblue))] in the one SCALE statement mapping colors.

For changing the background color, that has to do with your personal chart template, see a tutorial about how to change the background color in the template here http://andrewpwheeler.wordpress.com/2012/01/03/hacking-the-default-spss-chart-template/ (or you can set it via the editor menu).

*****************************************************.
*Fake data.
set seed 10.
input program.
loop #i = 1 to 20.
  loop #j = 1 to 40.
    compute area = #i.
    compute class = RV.BERNOULLI(area/20).
    end case.
  end loop.
end loop.
end file.
end input program.
dataset name sim.
formats area (F2.0).
variable level area class (nominal).
exe.

*Stacked Bar Graph.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=area COUNT()[name="COUNT"]
  class MISSING=LISTWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
 SOURCE: s=userSource(id("graphdataset"))
 DATA: area=col(source(s), name("area"), unit.category())
 DATA: COUNT=col(source(s), name("COUNT"))
 DATA: class=col(source(s), name("class"), unit.category())
 GUIDE: axis(dim(1), label("area"))
 GUIDE: axis(dim(2), label("Percent"))
 GUIDE: legend(aesthetic(aesthetic.color.interior), label("class"))
 SCALE: cat(dim(1))
 SCALE: linear(dim(2), include(0))
 SCALE: cat(aesthetic(aesthetic.color.interior), map(("0", color.blue), ("1", color.darkblue)))
 ELEMENT: interval.stack(position(summary.percent(area*COUNT,
  base.coordinate(dim(1)))), color.interior(class), shape.interior(shape.square))
END GPL.
*****************************************************.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Correcting syntax to obtain a stacked bar graph

bgreen
Andy,

Many thanks for the syntax.

There are two aspects that I would appreciate advice on:

1) rather than a graph where all columns equal 100%, I 'd like a
count, e.g if area A consisted of 20 individuals in class Low and 10
in class High and B 35 in class Low and 2 in class High etc, then the
total bar height for A would be 30 and B and 37 respectively.

2) The legend has values of .00 and 1.00 beside the respective
colours. What change is required to include "low" and "high". I could
alter the values in the data set but I presume there is a more elegant way.

Thanks once again.

Bob




At 10:48 PM 13/09/2013, Andy W wrote:

>Bob,
>
>Please in the future start a new thread, don't reply to other emails
>(archiving in NABBLE gets messed up). This is pretty easy to do right from
>the GGRAPH menu, the only thing I had to add was [, map(("0", color.blue),
>("1", color.darkblue))] in the one SCALE statement mapping colors.
>
>For changing the background color, that has to do with your personal chart
>template, see a tutorial about how to change the background color in the
>template here
>http://andrewpwheeler.wordpress.com/2012/01/03/hacking-the-default-spss-chart-template/
>(or you can set it via the editor menu).
>
>*****************************************************.
>*Fake data.
>set seed 10.
>input program.
>loop #i = 1 to 20.
>   loop #j = 1 to 40.
>     compute area = #i.
>     compute class = RV.BERNOULLI(area/20).
>     end case.
>   end loop.
>end loop.
>end file.
>end input program.
>dataset name sim.
>formats area (F2.0).
>variable level area class (nominal).
>exe.
>
>*Stacked Bar Graph.
>GGRAPH
>   /GRAPHDATASET NAME="graphdataset" VARIABLES=area COUNT()[name="COUNT"]
>   class MISSING=LISTWISE REPORTMISSING=NO
>   /GRAPHSPEC SOURCE=INLINE.
>BEGIN GPL
>  SOURCE: s=userSource(id("graphdataset"))
>  DATA: area=col(source(s), name("area"), unit.category())
>  DATA: COUNT=col(source(s), name("COUNT"))
>  DATA: class=col(source(s), name("class"), unit.category())
>  GUIDE: axis(dim(1), label("area"))
>  GUIDE: axis(dim(2), label("Percent"))
>  GUIDE: legend(aesthetic(aesthetic.color.interior), label("class"))
>  SCALE: cat(dim(1))
>  SCALE: linear(dim(2), include(0))
>  SCALE: cat(aesthetic(aesthetic.color.interior), map(("0", color.blue),
>("1", color.darkblue)))
>  ELEMENT: interval.stack(position(summary.percent(area*COUNT,
>   base.coordinate(dim(1)))), color.interior(class),
>shape.interior(shape.square))
>END GPL.
>*****************************************************.
>
>
>
>-----
>Andy W
>[hidden email]
>http://andrewpwheeler.wordpress.com/
>--
>View this message in context:
>http://spssx-discussion.1045642.n5.nabble.com/Correcting-syntax-to-obtain-a-stacked-bar-graph-tp5721956p5721961.html
>Sent from the SPSSX Discussion mailing list archive at 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

=====================
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: Correcting syntax to obtain a stacked bar graph

Andy W
For 2), you need to have value labels (which will be propogated to the legend labels). For 1) you can just take out the code associated with "stack.percent()" which includes the base.coordinate dim part with all the parentheses. Again, this all can be accomplished through the GGRAPH builder GUI (you can choose either stack counts or stack percents) - the only part I added was the mapping colors part.

Using slightly different fake data syntax.

*****************************************************.
*Fake data.
set seed 10.
input program.
loop #i = 1 to 20.
  loop #j = 1 to TRUNC(RV.UNIFORM(20,41)).
    compute area = #i.
    compute class = RV.BERNOULLI(area/20).
    end case.
  end loop.
end loop.
end file.
end input program.
dataset name sim.
formats area (F2.0).
variable level area class (nominal).
exe.

*For legend need to have value labels.
value labels class
0 'Low'
1 'High'.

*For bars to sum to count, take out "summary.percent()" part of ELEMENT statement.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=area COUNT()[name="COUNT"]
  class MISSING=LISTWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
 SOURCE: s=userSource(id("graphdataset"))
 DATA: area=col(source(s), name("area"), unit.category())
 DATA: COUNT=col(source(s), name("COUNT"))
 DATA: class=col(source(s), name("class"), unit.category())
 GUIDE: axis(dim(1), label("area"))
 GUIDE: axis(dim(2), label("Percent"))
 GUIDE: legend(aesthetic(aesthetic.color.interior), label("class"))
 SCALE: cat(dim(1))
 SCALE: linear(dim(2), include(0))
 SCALE: cat(aesthetic(aesthetic.color.interior), map(("0", color.blue), ("1", color.darkblue)))
 ELEMENT: interval.stack(position(area*COUNT), color.interior(class), shape.interior(shape.square))
END GPL.
*****************************************************.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Correcting syntax to obtain a stacked bar graph

bgreen
Hello Andy,

Thanks again. This worked perfectly.

Regards

Bob





At 10:18 PM 16/09/2013, Andy W wrote:

>For 2), you need to have value labels (which will be propogated to the legend
>labels). For 1) you can just take out the code associated with
>"stack.percent()" which includes the base.coordinate dim part with all the
>parentheses. Again, this all can be accomplished through the GGRAPH builder
>GUI (you can choose either stack counts or stack percents) - the only part I
>added was the mapping colors part.
>
>Using slightly different fake data syntax.
>
>*****************************************************.
>*Fake data.
>set seed 10.
>input program.
>loop #i = 1 to 20.
>   loop #j = 1 to TRUNC(RV.UNIFORM(20,41)).
>     compute area = #i.
>     compute class = RV.BERNOULLI(area/20).
>     end case.
>   end loop.
>end loop.
>end file.
>end input program.
>dataset name sim.
>formats area (F2.0).
>variable level area class (nominal).
>exe.
>
>*For legend need to have value labels.
>value labels class
>0 'Low'
>1 'High'.
>
>*For bars to sum to count, take out "summary.percent()" part of ELEMENT
>statement.
>GGRAPH
>   /GRAPHDATASET NAME="graphdataset" VARIABLES=area COUNT()[name="COUNT"]
>   class MISSING=LISTWISE REPORTMISSING=NO
>   /GRAPHSPEC SOURCE=INLINE.
>BEGIN GPL
>  SOURCE: s=userSource(id("graphdataset"))
>  DATA: area=col(source(s), name("area"), unit.category())
>  DATA: COUNT=col(source(s), name("COUNT"))
>  DATA: class=col(source(s), name("class"), unit.category())
>  GUIDE: axis(dim(1), label("area"))
>  GUIDE: axis(dim(2), label("Percent"))
>  GUIDE: legend(aesthetic(aesthetic.color.interior), label("class"))
>  SCALE: cat(dim(1))
>  SCALE: linear(dim(2), include(0))
>  SCALE: cat(aesthetic(aesthetic.color.interior), map(("0", color.blue),
>("1", color.darkblue)))
>  ELEMENT: interval.stack(position(area*COUNT), color.interior(class),
>shape.interior(shape.square))
>END GPL.
>*****************************************************.
>
>
>
>-----
>Andy W
>[hidden email]
>http://andrewpwheeler.wordpress.com/
>--
>View this message in context:
>http://spssx-discussion.1045642.n5.nabble.com/Correcting-syntax-to-obtain-a-stacked-bar-graph-tp5721956p5722041.html
>Sent from the SPSSX Discussion mailing list archive at 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

=====================
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