A Spaghetti Plot with a Twist

Recently, a user on the Graphics Communities page had a use case to graph 36 series groups together with a legend. The problem was the display of a legend with 36 entries was dropped as it exceeded the space normally allowed for a legend (20%) so the plot can still be displayed.

Multiple solutions to this issue were suggested in the thread: 1. Increase the amount of space allowed for the legend 2. Reduce the length of the line items in the legend. 3. Separate the Month and Year values, and use the GROUPLC and GROUPLP options to classify the curves by month and year.

I used the data provided by user to see the results. After viewing the results, another option would be to place the legend inside the empty portion of the graph area.

Reducing the line length, value font size and increasing the allowable space for legend, we get the following results.

title “Vintage by Seniority by Period”;
ods graphics / reset maxlegendarea=50 ;
proc sgplot data=vintage noborder;
series x=seniority y=vintage /group=fin_period;
xaxis display=(nolabel noline) integer values=(2 to 36) grid;
yaxis display=(nolabel noline) grid integer;
keylegend / title=” noborder across=6 linelength=32
valueattrs=(size=6);
run;

Note, even with the reduced line length for the items, the legend is quite large and takes away from the space available for the graph. We notice there is free space available at the lower right of the graph area, so we can place the legend in that space ordered by year per column, thus providing more space for the data plot.

Grouped series plot with legend inside.

title “Vintage by Seniority by Period”;
proc sgplot data=vintage noborder;
series x=seniority y=vintage /group=fin_period;
xaxis display=(nolabel noline) integer values=(2 to 36) grid;
yaxis display=(nolabel noline) grid integer;
keylegend / title=” noborder down=12 linelength=32
valueattrs=(size=6) location=inside position=bottomright;
run;

Placing the legend inside the data area with reduced value font size and line length allows more space for the plot data. However, the legend is still quite cluttered, and it is not easy to understand the information.

The final step is to break up the “fin_period” into its “Year” and “Month” components in new columns. Now, we can use the GroupLC option to color the series by Month, and GroupMS option to set the markers by Year. We also note the series for Year=2016 span over 36 months, while 2017 span 24 months and 2018 span 12 months. So, we can use the appropriate marker symbols to ensure the shorter series are visible in the more dense area.

Series by Year and Month

title “Vintage by Seniority by Period”;
proc sgplot data=vintage noborder;
styleattrs datasymbols=(plus trianglefilled circlefilled);
series x=seniority y=vintage /group=fin_period
markers markerattrs=(size=5)
grouplc=monthc groupms=yearc lineattrs=(pattern=solid);
xaxis display=(nolabel noline) integer values=(2 to 36) grid;
yaxis display=(nolabel noline) grid integer;
keylegend / title=’Month’ type=linecolor location=inside
noborder across=1 position=right;
keylegend / title=’Year’ type=markersymbol noborder;
run;

This graph is much easier to decode, with cleaner legends. Individual series are colored by month using the GroupLC option using only the solid line pattern. So, the “Month” legend only needs color, and can be placed inside the data area on the right. The markers for each series are classified by Year using the GroupMS option. Note the use of darker filled circles for the shorter series to make them stand out in the busy area. This is done using the StyleAttrs statement. A more robust solution would be using the DiscreteAttrMap feature.

I did try using GroupLP to classify the curves by Year, but the results were less than optimal. Then, I tried GroupMS, using markers that, imho, seems to provide better results.

We can certainly add axis labels and other details to improve the graphs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: