Graph Tips – VIII: Multiple Graphs

This example shows how to put multiple graphs together side by side. The graph in this example is about showing how whilst the unemployment rate can be misleading (eg. unemployment goes up not because there are fewer jobs, but because more people start looking for jobs) by-and-large this is not a problem and the unemployment rate and number of unemployed fluctuate together (at least in New Zealand).

NZ_LabourForce

Matlab code (trick is ‘range’ in axes):

% Employed Population: Aged 15 and Over: All Persons for New Zealand (Persons, Quarterly, Seasonally Adjusted)
fred_EmpPop = getFredData(‘LFEMTTTTNZQ647S’, StartDate, EndDate)
% Unemployed Population: Aged 15 and Over: All Persons for New Zealand (Persons, Quarterly, Seasonally Adjusted)
fred_UnempPop = getFredData(‘LFUNTTTTNZQ647S’, StartDate, EndDate)
% Working Age Population: Aged 15 and Over: All Persons for New Zealand (Persons, Quarterly, Seasonally Adjusted)
fred_WorkingAgePop = getFredData(‘LFWATTTTNZQ647S’, StartDate, EndDate)

trace1= struct(‘x’, {cellstr(datestr(fred_EmpPop.Data(:,1),’yyyy-QQ’))},’y’,fred_EmpPop.Data(:,2),’name’, ‘Employed Population’,’type’, ‘scatter’,’fill’,’tozeroy’,’line’, struct(‘color’, ‘hsv(210,80,100)’));
trace2= struct(‘x’, {cellstr(datestr(fred_UnempPop.Data(:,1),’yyyy-QQ’))},’y’,fred_EmpPop.Data(:,2)+fred_UnempPop.Data(:,2),’name’, ‘Unemployed Population’,’type’, ‘scatter’,’fill’,’tonexty’,’line’, struct(‘color’, ‘hsv(30,80,100)’));
trace3= struct(‘x’, {cellstr(datestr(fred_WorkingAgePop.Data(:,1),’yyyy-QQ’))},’y’,fred_WorkingAgePop.Data(:,2),’name’, ‘Working Age Population’,’type’, ‘scatter’,’line’, struct(‘color’, ‘hsv(90,100,80)’));
trace4= struct(‘x’, {cellstr(datestr(fred_Unemp.Data(:,1),’yyyy-QQ’))},’y’,fred_Unemp.Data(:,2),’name’, ‘Unemployment Rate’,’type’, ‘scatter’,’yaxis’,’y2′,’line’, struct(‘color’, ‘hsv(0,80,100)’));
data = {trace4,trace1,trace2,trace3};
layout = struct(‘title’, ‘Labour Force Population in New Zealand (Ages 15+)’,’showlegend’, true,’width’, 800,…
‘xaxis’, struct(‘title’,’Year’,’showgrid’,false,’anchor’,’y2′,’tickangle’,-45,’nticks’,20), …
‘yaxis’, struct(‘title’, ‘Persons’,’titlefont’, struct(‘color’, ‘black’),’tickfont’, struct(‘color’, ‘black’),’position’,0, ‘domain’, [0.25, 1],’anchor’, ‘free’,’showgrid’,false),…
‘yaxis2’, struct(‘title’,’Percent’,’titlefont’, struct(‘color’, ‘black’),’tickfont’, struct(‘color’, ‘black’),’position’,0, ‘domain’, [0, 0.25],’anchor’, ‘free’,’showgrid’,false),…
‘legend’, struct(‘traceorder’,’reversed’) );%, …
response = plotly(data, struct(‘layout’, layout, ‘filename’, ‘NZ_LabourForce’, ‘fileopt’, ‘overwrite’));
response.data=data; response.layout=layout;
saveplotlyfig(response, ‘./Graphs/NZ_LabourForce.pdf’)