SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
Expand all | Collapse all

Generate stacked bar charts including empty categories in batch

  • 1.  Generate stacked bar charts including empty categories in batch

    Posted Fri March 19, 2021 10:48 PM

    I have multiple dependent variables of which I want to plot a stacked bar chart, where the groups are in each plot determined by one of multiple dependent variables. I would like to do this in batch (I do not want to use the chart builder interface 100 times).

    I have tried to define a macro like this:

    define G (!POS !CHAREND('/') / !POS !TOKENS(1)) !do !i !in (!1) graph /bar(stacked)=pct by !i by !2. !doend !enddefine.

    Which works, but the resulting graphs don't show categories on the x axes with 0 frequency, which I do want.

    With the chart builder interface it is easy to obtain what I want, but I don't want to do this myself for all combinations of all dependent and independent variables. The resulting syntax is something like this:

    * Chart Builder. GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=Q23_1 Q9_3b20 MISSING=VARIABLEWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: Q23_1=col(source(s), name("Q23_1")) DATA: Q9_3b20=col(source(s), name("Q9_3b20"), notIn("4"), unit.category()) GUIDE: axis(dim(1), label("Q23 Huidige algemene kennis")) GUIDE: axis(dim(2), label("Frequency")) GUIDE: legend(aesthetic(aesthetic.color.interior), label("Q9_3 Leeftijd (Binned)")) GUIDE: text.title(label("Stacked Bar of Q23 Huidige algemene kennis by Q9_3 Leeftijd (Binned)")) SCALE: linear(dim(1), min(0), max(10)) SCALE: cat(aesthetic(aesthetic.color.interior), include( "1", "2", "3"), aestheticMissing(color.black)) ELEMENT: interval.stack(position(summary.count(bin.rect(Q23_1))), color.interior(Q9_3b20), shape.interior(shape.square)) END GPL.

    Which I cannot put into a macro because of the inline GPL. I can't put the GPL in a separate file (as suggested here), because the 'Q23_1' and 'Q9_3b20' would have to be replaced by macro variable names.

    So, is there any way to achieve what I want? Is there a way to tell the 'GRAPH' command to add empty categories to the X axis? Or is there a way to pass variables to an external GPL file? Or another way to do this?

    Thank you in advance!






    #SPSSStatistics
    #Support
    #SupportMigration


  • 2.  RE: Generate stacked bar charts including empty categories in batch

    Posted Fri March 19, 2021 11:01 PM

    GRAPH will only include categories that appear in the data. As you observe, GGRAPH can do this. The best way to do what you want would be to embed the GPL in a little Python function with two arguments. I can send you the code for this if you send me a sav file for testing (jkpeckSupport Member).






    #SPSSStatistics
    #Support
    #SupportMigration


  • 3.  RE: Generate stacked bar charts including empty categories in batch

    Posted Sat March 20, 2021 06:01 PM

    Thank you that would be nice! I'll send you an email :-).






    #SPSSStatistics
    #Support
    #SupportMigration