How to Add Stacking to a Graph (GPL)
Stacking involves a couple of changes to the ELEMENT
statement.
The following steps use the GPL shown in The Basics (GPL)
as a "baseline" for the changes.
- Before modifying the
ELEMENT
statement, you need to define an additional categorical variable that will be used for stacking. This is specified by aDATA
statement (note theunit.category()
function):DATA: gender=col(source(s), name("gender"), unit.category())
- The first change to the
ELEMENT
statement will split the graphic element into color groups for each gender category. This splitting results from using thecolor
function:ELEMENT: interval(position(summary.mean(jobcat*salary)), color(gender))
- Because there is no collision modifier for the interval element, the groups of bars are overlaid on each other, and there's no way to distinguish them. In fact, you may not even see graphic elements for one of the groups because the other graphic elements obscure them. You need to add the stacking collision modifier to re-position the groups (we also changed the statistic because stacking summed values makes more sense than stacking the mean values):
ELEMENT: interval.stack(position(summary.sum(jobcat*salary)), color(gender))
The complete GPL is shown below:
SOURCE: s = userSource(id("Employeedata"))
DATA: jobcat = col(source(s), name("jobcat"), unit.category())
DATA: gender = col(source(s), name("gender"), unit.category())
DATA: salary = col(source(s), name("salary"))
SCALE: linear(dim(2), include(0.0))
GUIDE: axis(dim(2), label("Sum Salary"))
GUIDE: axis(dim(1), label("Job Category"))
ELEMENT: interval.stack(position(summary.sum(jobcat*salary)), color(gender))
Following is the graph created from the GPL.

Legend Label
The graph includes a legend,
but it has no label by default. To add or change the label for the
legend, you use a GUIDE
statement:
GUIDE: legend(aesthetic(aesthetic.color), label("Gender"))