SPSS - Clustered Bar Chart for Multiple Variables
SPSS tutorials website header logo SPSS TUTORIALS VIDEO COURSE BASICS ANOVA REGRESSION FACTOR

Clustered Bar Chart over Multiple Variables

This tutorial shows how to create the clustered bar chart shown below in SPSS. As this requires restructuring our data, we'll first do so with a seriously cool trick.

SPSS Clustered Bar Chart Example

Example Data

A sample of N = 259 respondents were asked “which means of transportation do you use on a regular basis?” Respondents could select one or more means of transportation for both work and leisure related travelling. The data thus obtained are in transportation.sav, partly shown below.

SPSS Varstocases Example Data 208

Note that our data consist of 2 sets (work and leisure) of 8 dichotomous variables (transportation options). For creating our chart, we need to stack these 2 sets vertically. The usual way to do so is with VARSTOCASES as illustrated below.

SPSS VARSTOCASES Example Diagram

Now, we could use VARSTOCASES for our data but we find this rather tedious for 8 variables. We'll replace it with a trick that creates nicer results and requires less syntax too.

VARSTOCASES without VARSTOCASES

The screenshots below illustrate how to mimic VARSTOCASES without needing its syntax.

SPSS Varstocases Blog 208

This method saves more effort insofar as you stack more variables: placing the IF command in step in a DO REPEAT loop does the trick.

Restructuring the Data

Let's now run these steps on transportation.sav with the syntax below. If you're not sure about some command, you can inspect its result in data view if you run EXECUTE right after it.

*Create checktable 1.
descriptives all.

*Add id to dataset.
compute id = $casenum.

*Vertically stack dataset onto itself.
add files file */file *.

*Identify copied cases.
compute Purpose = ($casenum > id).
value labels Purpose 0 'Work' 1 'Leisure'.

*Shift values from leisure into work variables.
do repeat #target = work_1 to work_8 / #source = leis_1 to leis_8.
if(purpose) #target = #source.
end repeat.

*Create checktable 2.
means work_1 to work_8 by Purpose.

If everything went right, the 2 checktables will show the exact same information.

Final Data Adjustments

Our data now have the structure required for running our chart. However, a couple of adjustments are still desirable:

*Convert 1 into 100 for percentages.
recode work_1 to work_8 (1 = 100).

*Set formats to percentages.
formats work_1 to work_8 (pct8).

*Set measurement levels (needed for SPSS chart builder & Custom Tables).
variable level work_1 to work_8 (scale).

*Remove (now) redundant variables.
add files file */drop leis_1 to id.

SPSS Chart Builder - Basic Steps

The screenshot below sketches some basic steps that'll result in our chart.

SPSS Clustered Bar Chart Dialog 1

drag and drop the clustered bar chart onto the canvas;
select, drag and drop all outcome variables in one go into the y-axis box. Click “Ok” in the dialog that pops up;
drag “Purpose” (leisure or work) into the Color box;
go through these tabs, select “Transpose” and choose some title and subtitle for the chart;
clicking Paste results in the syntax below.

*Syntax pasted from SPSS Chart Builder.

GGRAPH
/GRAPHDATASET NAME="graphdataset" VARIABLES=MEAN(work_1) MEAN(work_2) MEAN(work_3) MEAN(work_4)
    MEAN(work_5) MEAN(work_6) MEAN(work_7) MEAN(work_8) Purpose MISSING=LISTWISE REPORTMISSING=NO
    TRANSFORM=VARSTOCASES(SUMMARY="#SUMMARY" INDEX="#INDEX")
/GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
SOURCE: s=userSource(id("graphdataset"))
DATA: SUMMARY=col(source(s), name("#SUMMARY"))
DATA: INDEX=col(source(s), name("#INDEX"), unit.category())
DATA: Purpose=col(source(s), name("Purpose"), unit.category())
COORD: rect(dim(1,2), transpose(), cluster(3,0))
GUIDE: axis(dim(2), label("Mean"))
GUIDE: legend(aesthetic(aesthetic.color.interior), label("Purpose"))
GUIDE: text.title(label("Transportation Methods Used on Regular Basis"))
GUIDE: text.subsubtitle(label("All Respondents | N = 259"))
SCALE: cat(dim(3), reverse(), include("0", "1", "2", "3", "4", "5", "6", "7"))
SCALE: linear(dim(2), include(0))
SCALE: cat(aesthetic(aesthetic.color.interior), reverse(), include("0.00", "1.00"))
SCALE: cat(dim(1), include("0.00", "1.00"))
ELEMENT: interval(position(Purpose*SUMMARY*INDEX), color.interior(Purpose),
    shape.interior(shape.square))
END GPL.

Initial Result

SPSS Clustered Bar Chart Unstyled 208

Whoomp! There it is. We created the chart we were looking for. Sadly, it doesn't look too great:

All such issues can be fixed in the Chart Editor which opens if we double-click our chart. A nicer option, though, is developing and applying a chart template. Our final result after doing so is shown below.

Final Result

SPSS Clustered Bar Chart Example

Right. This tutorial has been pretty heavy on syntax but I hope you got the hang of it. Were you (not) able to recreate our chart? Do you have any other feedback? Please throw us a comment below.

Thanks for reading!

Creating Bar Charts with Means by Category

One of the most common research questions is do different groups have different mean scores on some variable? This question is best answered in 3 steps:

  1. create a table showing mean scores per group -you'll probably want to include the frequencies and standard deviations as well;
  2. create a chart showing mean scores per group;
  3. run some statistical test -ANOVA in this case. However, this is only meaningful if your data are (roughly) a simple random sample from your target population.

We'll show the first 2 steps using an employee survey whose data are in bank-clean.sav. The screenshot below shows what these data basically look like.

SPSS Data View Bank Clean

Creating a Means Table

For creating a table showing means per category, we could mess around with Analyze SPSS Menu Arrow Compare Means SPSS Menu Arrow Means but its not worth the effort as the syntax is as simple as it gets. So let's just run it and inspect the result.

*Create table with mean ratings by job type.

means q1 by jtype
/cells count mean stddev.

Note that you could easily add more statistics to the CELLS subcommand such as

Result

SPSS Bar Chart Means By Category Means Table

Basically, our table tells us that the mean employee care ratings increase with higher job levels except for “upper management”. A proper descriptives table -always recommended- gives nicely detailed information. However, it's not very visual. So let's now run our chart.

SPSS Bar Chart Menu & Dialogs

As a rule of thumb, I create all charts from Graphs SPSS Menu Arrow Legacy Dialogs and avoid the Chart Builder whenever I can -which is pretty much always unless I need a stacked bar chart with percentages.

SPSS Graphs Legacy Dialogs Bar Menu

In the dialog shown below, selecting Other statistic... enables you to enter a dependent variable;

SPSS Bar Chart Means By Category Dialog

Basic Bar Chart Means by Category Syntax

*Bar chart means by category syntax.

GRAPH
/BAR(SIMPLE)=MEAN(q1) BY jtype
/TITLE='Mean Employee Care Rating by Job Type'
/SUBTITLE='N = 423'.

Result

SPSS Bar Chart Means By Category With Chart Template

We now have our basic chart but it doesn't look too good.From SPSS version 25 onwards, it will look somewhat better. However, see New Charts in SPSS 25 - How Good Are They Really? Yet. We'll fix this by setting a chart template.

Note that a chart template can also sort this bar chart -for instance by descending means- but we'll skip that for now.

Bar Chart Means by Category Syntax II

*Set chart template (make sure the .sgt file is in default folder as shown by SHOW DIR.).

set ctemplate "bar-chart-means-trans-720-1.sgt".

*Rerun chart with template set.

GRAPH
/BAR(SIMPLE)=MEAN(q1) BY jtype
/TITLE='Mean Employee Care Rating by Job Type'
/SUBTITLE='N = 423'.

Result

SPSS Bar Chart Means By Category With Chart Template

Right, so that's about it. If you need a couple of similar charts, you could copy-paste-edit the last GRAPH command (no need to repeat the other commands). If you need many similar charts, you could loop over the GRAPH command with Python for SPSS.

If you're going to run other types of charts, don't forget to set a different chart template. Or switch if off altogether by running set ctemplate none.

Thanks for reading!

SPSS – Stacked Bar Charts Percentages

SPSS Stacked Bar Chart - Example with Template Applied

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.

Screenshot of Practice Data File

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

SPSS Stacked Bar Chart - Crosstab

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

SPSS Stacked Bar Chart - Menu SPSS Stacked Bar Chart - Select Basic Chart SPSS Stacked Bar Chart - Set Measurement Level

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.

SPSS Stacked Bar Chart - Add Variables SPSS Stacked Bar Chart - Transpose Chart

Transpose 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.

SPSS Stacked Bar Chart - Select Percentages 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 Apply whenever changing something in the Element Properties dialog (we forget it all the time).

SPSS Stacked Bar Chart - Add Main Title And again: “Apply”.

Optionally, set a main title for the chart and Apply it. Clicking Paste 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

SPSS Stacked Bar Chart - Unstyled

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

SPSS Stacked Bar Chart - Styled

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!

Thanks for reading!

SPSS Bar Charts Tutorial

SPSS Bar Chart Styled

One of the best known charts is a simple bar chart containing frequencies or percentages. The easiest way to run it in SPSS is the FREQUENCIES command. This tutorial walks you through some options. We'll use freelancers.sav throughout, part of which is shown below.

SPSS Bar Chart Tutorial Data View

Option 1: FREQUENCIES

In most cases, a simple FREQUENCIES command is our best option because it takes multiple variables in one go. Its syntax -shown below- is so simple that just typing it is probably faster than messing around with the menu.

*Easiest way to run 5 bar charts.

frequencies sector_2010 to sector_2014
/format notable
/barchart.

Result - Basic

SPSS Bar Chart Unstyled 600

Unsurprisingly, we created the desired bar charts but -like most SPSS charts- they look awful. A great way to fix that is using an SPSS chart template. Just one template is sufficient for having pretty bar charts for once and for all.
A chart template can also transpose (“put on its side”) our chart, which works much better for bar charts than SPSS’ default orientation. One of the charts that resulted from the template we built is shown below.

Result - Chart Template

SPSS Bar Chart Styled

Sorting Categories and Percentages

A nice -albeit little known- option is sorting the categories. If we transpose our chart, we can sort our bars by descending frequency by specifying /FORMAT NOTABLE AFREQ (use DFREQ for untransposed charts).
Second, we can have percentages instead of frequencies by using /BARCHART PERCENT as shown in the syntax below.

Sorted Bar Charts with Percentages Syntax

*Bar chart percentages, sorted by ascending frequency ("afreq").

frequencies sector_2010
/format notable afreq
/barchart percent.

Result

SPSS Bar Chart Percentages Styled

Note: the %-sign is missing from our chart but we can fix this by modifying our chart template.

Option 2: GRAPH

When we run bar charts with FREQUENCIES, the variable labels of the variables involved are used as chart titles. If we want custom titles instead, we're perhaps better off by using GRAPH.Alternatively, set the desired chart titles as new variable labels. Preceding this with TEMPORARY restores the old labels. Since its syntax is a bit more difficult, we'll generate it from the menu as shown below.

SPSS Graphs Legacy Dialogs Bar 840 SPSS Bar Chart Frequencies Legacy Dialog

Resulting Syntax

*Bar chart syntax from legacy dialog.

GRAPH
/BAR(SIMPLE)=PCT BY sector_2010
/TITLE='All Respondents | n = 40'.

Note: unfortunately, GRAPH takes only one variable at the time. However, we can remove the line breaks from the syntax and copy-paste-edit it a couple of times for a handful of variables. For running charts or tables over many variables, see SPSS with Python - Looping over Scatterplots.
Second, GRAPH does not allow us to sort our categories but a chart template can fix that.

SPSS Bar Charts - Chart Sizes

One issue with all SPSS charts is that their sizes are fixed in pixels. However, a bar chart for many categories needs more space than a chart for few categories. In fact, categories may disappear altogether if they don't fit into the chart anymore. SPSS does not offer a solution for this other than “stretching” each chart manually in the output viewer. For a better solution, see SPSS - Set Chart Sizes Tool.
With respect to the layout of reports, we prefer having the heights (rather than the widths) of our charts depend on the amount of content they contain. This is yet another good reason for always transposing our bar charts.

Conclusion

In most cases, typing a simple FREQUENCIES command is by far the best option for creating bar charts. GRAPH as pasted from Graphs SPSS Menu Arrow Legay Dialogs is a reasonable option too.
As with most charts, Graphs SPSS Menu Arrow Chart Builder is better avoided since it's way more complicated and results in the exact same chart as the aforementioned options.

Bar Charts - FREQUENCIES Versus GRAPH

The table below quickly summarizes the differences between the two options we discussed in this tutorial.

FeatureFREQUENCIESGRAPH
Multiple variablesYesNo
Category sortingYesOnly with chart template
Percent signOnly with chart templateYes
Custom titleNoYes