SPSS TUTORIALS FULL COURSE BASICS ANOVA REGRESSION FACTOR

# SPSS – Stacked Bar Charts Percentages

Creating SPSS stacked bar charts with percentages -as shown above- is pretty easy. However, figuring out the right steps may take quite some effort and frustration. This tutorial therefore shows how to do it properly in one go.
We encourage you to follow along on course_evaluation.sav. Part of these data are shown in the screenshot below.

## Example: Course Rating by Study Major

Let's say we'd like to visualize the association between study major (nominal) and overall course rating (ordinal). A table that gives us some insight is a contingency table showing column percentages. We'll create it by running the syntax below.

*Show only labels in output.

set tnumbers labels tvars labels.

*Crosstab with column percentages.

crosstabs q1 by major
/cells column.

## Result

Our course was least popular with students studying some “Other” study major. Can you tell which students like our course most?
Anyway, it's exactly this table that we'll visualize as a chart. We'll do so by following the next five screenshots.

## SPSS Chart Builder Dialogs

Our stacked bar chart requires setting measurement levels to nominal or ordinal. You could do so before opening the chart builder (possibly preceded by TEMPORARY) or within the chart builder. When using this second option, the chosen measurement levels apply only to the chart you're creating.

sort of rotates our chart by 90 degrees and thus changes the chart layout from vertical to horizontal. This isn't necessary but the horizontal layout is usually much more suitable for all sorts of bar charts than the default vertical layout.
Two options for transposing charts are 1) in the chart builder as we do now or 2) by applying an SPSS chart template.

Don't forget to “Apply” here -like I always do.

The steps in the screenshot above show the steps for selecting the right percentages for this chart. Don't forget to click whenever changing something in the Element Properties dialog (we forget it all the time).

And again: “Apply”.

Optionally, set a main title for the chart and it. Clicking results in the syntax below. Let's run it.

## SPSS Stacked Bar Chart Syntax

* Chart Builder.
GGRAPH
/GRAPHDATASET NAME="graphdataset" VARIABLES=major COUNT()[name="COUNT"] q1[LEVEL=ORDINAL]
MISSING=LISTWISE REPORTMISSING=NO
/GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
SOURCE: s=userSource(id("graphdataset"))
DATA: major=col(source(s), name("major"), unit.category())
DATA: COUNT=col(source(s), name("COUNT"))
DATA: q1=col(source(s), name("q1"), unit.category())
COORD: rect(dim(1,2), transpose())
GUIDE: axis(dim(1), label("What's currently your (primary) major?"))
GUIDE: axis(dim(2), label("Percent"))
GUIDE: legend(aesthetic(aesthetic.color.interior), label("How do you rate this course?"))
GUIDE: text.title(label("Course Ratings by Study Major"))
SCALE: cat(dim(1), include("1", "2", "3", "4", "5"))
SCALE: linear(dim(2), include(0))
SCALE: cat(aesthetic(aesthetic.color.interior), include("1", "2", "3", "4", "5"))
ELEMENT: interval.stack(position(summary.percent(major*COUNT, base.coordinate(dim(1)))),
color.interior(q1), shape.interior(shape.square))
END GPL.

## Unstyled Stacked Bar Chart

First, note that “Very bad” appears in our legend even though it's not present in our data. By default, the Chart Builder includes all values for which value labels are present regardless whether they are present in the data. This can be very annoying: any categories you excluded with FILTER now reappear in your chart.
Second, our chart looks terrible (however, see New Charts in SPSS 25 - How Good Are They Really?). However, a chart template is a great way to fix that. The end result is shown below.

## Styled Stacked Bar Chart

Unfortunately, our chart doesn't show any association at all -a bit of an anticlimax after all the work. But I hope you'll have more luck with your charts!

# Tell us what you think!

*Required field. Your comment will show up after approval from a moderator.

# THIS TUTORIAL HAS 12 COMMENTS:

• ### By Ruben Geert van den Berg on June 1st, 2018

Thanks! Now I see what you mean. I kinda prefer the stacked bars, though. Hope you don't mind!

• ### By Engliterra on June 3rd, 2018

Thanks so much for the post. Much thanks again. Really Cool.

• ### By Ruben Geert van den Berg on June 3rd, 2018

Happy to hear you like it! Keep up the good work!

• ### By Michelle W on June 22nd, 2020

This was super helpful! One follow up question-- is there a way to label each portion of the stacked bars with the percent they represent? Also how did you get the n to show for each major category?

• ### By Ruben Geert van den Berg on June 23rd, 2020

Hi Michelle, thanks for the compliment!

"is there a way to label each portion of the stacked bars with the percent they represent?"

Well, we built a tool for that in 2016 but it took us some 20(!) hours of SPSS/GPL/Python programming. The tool is not publicly available. So it's possible in theory but you don't really want to go there...

"how did you get the n to show for each major category?"

We manually added them to the value labels. The ADD VALUE LABELS command is the right way for doing so. We built a tool for that as well but we never published it because there's too little demand for it.

Hope that helps!

SPSS tutorials