How to Add Faceting (Paneling) to a Graph (GPL)

Faceted variables are added to the algebra in the ELEMENT statement. The following steps use the GPL shown in The Basics (GPL) as a "baseline" for the changes.

  1. Before modifying the ELEMENT statement, we need to define an additional categorical variable that will be used for faceting. This is specified by a DATA statement (note the unit.category() function):
    DATA: gender=col(source(s), name("gender"), unit.category())
  2. Now we add the variable to the algebra. We will cross the variable with the other variables in the algebra:
ELEMENT: interval(position(summary.mean(jobcat*salary*gender)))

Those are the only necessary steps. The final 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("Mean Salary"))
GUIDE: axis(dim(1), label("Job Category"))
ELEMENT: interval(position(summary.mean(jobcat*salary*gender)))

Following is the graph created from the GPL.

Figure 1. Faceted bar chart
Faceted bar chart

Additional Features

Labeling. If you want to label the faceted dimension, you treat it like the other dimensions in the graph by adding a GUIDE statement for its axis:

GUIDE: axis(dim(3), label("Gender"))

In this case, it is specified as the 3rd dimension. You can determine the dimension number by counting the crossed variables in the algebra. gender is the 3rd variable.

Nesting. Faceted variables can be nested as well as crossed. Unlike crossed variables, the nested variable is positioned next to the variable in which it is nested. So, to nest gender in jobcat, you would do the following:

ELEMENT: interval(position(summary.mean(jobcat/gender*salary)))

Because gender is used for nesting, it is not the 3rd dimension as it was when crossing to create facets. You can't use the same simple counting method to determine the dimension number. You still count the crossings, but you count each crossing as a single factor. The number that you obtain by counting each crossed factor is used for the nested variable (in this case, 1). The other dimension is indicated by the nested variable dimension followed by a dot and the number 1 (in this case, 1.1). So, you would use the following convention to refer to the gender and jobcat dimensions in the GUIDE statement:

GUIDE: axis(dim(1), label("Gender"))
GUIDE: axis(dim(1.1), label("Job Category"))
GUIDE: axis(dim(2), label("Mean Salary"))