Graph Tips – VII: Stacking and Shading

This graph shows how to stack lines and to do shading of areas. There is a lot of code importing all the data which you can largely skip past.

GreatRecession_FedReserveBalanceSheet

Matlab Code (the shading is done by, eg. ‘fill’,’tonexty’ in traces):

%% Federal Reserve Balance Sheet in the Great Recession
GRdate1=’2004-01-01′;
GRdate2=’2015-01-01′;

% All Federal Reserve Banks – Total Assets, Eliminations from Consolidation (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WALCL = getFredData(‘WALCL’, GRdate1, GRdate2)
trace1= struct(‘x’, {cellstr(datestr(fred_WALCL.Data(:,1),’yyyy-mm’))},’y’,fred_WALCL.Data(:,2),’name’, ‘Fed Reserve: Total Assets’,’type’, ‘scatter’);% ,’line’, struct(‘color’, ColorString));

% Mortgage-backed securities held by the Federal Reserve: All Maturities (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_MBST = getFredData(‘MBST’, GRdate1, GRdate2)
% Federal agency debt securities held by the Federal Reserve: All Maturities (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_FEDDT = getFredData(‘FEDDT’, GRdate1, GRdate2)
% Assets – Unamortized premiums on securities held outright (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WUPSHO = getFredData(‘WUPSHO’, GRdate1, GRdate2)
% Assets – Unamortized discounts on securities held outright (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WUDSHO = getFredData(‘WUDSHO’, GRdate1, GRdate2)
fred_FEDDT_MBST_ETC.Data=[fred_MBST.Data(:,1),fred_FEDDT.Data(:,2)+fred_MBST.Data(:,2)+fred_WUPSHO.Data(:,2)+fred_WUDSHO.Data(:,2)];
trace2= struct(‘x’, {cellstr(datestr(fred_MBST.Data(:,1),’yyyy-mm’))},’y’,fred_FEDDT_MBST_ETC.Data(:,2),’name’, ‘Fed Agency Debt + MBS’,’type’, ‘scatter’);% ,’line’, struct(‘color’, ColorString));
trace2_1= struct(‘x’, {cellstr(datestr(fred_MBST.Data(:,1),’yyyy-mm’))},’y’,fred_MBST.Data(:,2),’name’, ‘Mortgage Backed Securities’,’type’, ‘scatter’);% ,’line’, struct(‘color’, ColorString));
trace2_2= struct(‘x’, {cellstr(datestr(fred_FEDDT.Data(:,1),’yyyy-mm’))},’y’,fred_FEDDT.Data(:,2),’name’, ‘Fed Agency Debt’,’type’, ‘scatter’);% ,’line’, struct(‘color’, ColorString));

% Central Bank Liquidity Swaps held by the Federal Reserve: All Maturities (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_SWPT = getFredData(‘SWPT’, GRdate1, GRdate2)
trace3= struct(‘x’, {cellstr(datestr(fred_SWPT.Data(:,1),’yyyy-mm’))},’y’,fred_SWPT.Data(:,2),’name’, ‘Liquidity to Key Markets (Liq. Swaps)’,’type’, ‘scatter’);% ,’line’, struct(‘color’, ColorString));

% Lending to Banks
% Factors Supplying Reserve Balances – Loans (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WLCFLL = getFredData(‘WLCFLL’, GRdate1, GRdate2)
% Assets – Repurchase Agreements (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WARAL = getFredData(‘WARAL’, GRdate1, GRdate2)
% Factors Supplying Reserve Balances – Net Portfolio Holdings of TALF LLC (DISCONTINUED SERIES) (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WNPHTALFL = getFredData(‘WNPHTALFL’, GRdate1, GRdate2)
TALFcorrection=1
% Maiden Lane LLC are the bailouts to Bear Sterns and AIG.
% Factors Supplying Reserve Balances – Net Portfolio Holdings of Maiden Lane LLC (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WNPHML1L = getFredData(‘WNPHML1L’, GRdate1, GRdate2)
% Factors Supplying Reserve Balances – Net Portfolio Holdings of Maiden Lane II LLC (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WNPHML2L = getFredData(‘WNPHML2L’, GRdate1, GRdate2)
% Factors Supplying Reserve Balances – Net Portfolio Holdings of Maiden Lane III LLC (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WNPHML3L = getFredData(‘WNPHML3L’, GRdate1, GRdate2)
StandardLending=-Inf*ones(size(fred_WLCFLL.Data(:,2)));
for ii=1:size(fred_WLCFLL.Data,1)
date_c=fred_WLCFLL.Data(ii,1);
StandardLending(ii)=fred_WLCFLL.Data(ii,2);
for jj=1:5
if jj==1
fredData=fred_WARAL.Data;
elseif jj==2
clear fredData
fredData=[fred_WNPHTALFL.Data(:,1), fred_WNPHTALFL.Data(:,2)*TALFcorrection];
elseif jj==3
fredData=fred_WNPHML1L.Data;
elseif jj==4
fredData=fred_WNPHML2L.Data;
elseif jj==5
fredData=fred_WNPHML3L.Data;
end

[val,ind]= min(abs(fredData(:,1)-date_c));
if val==0
StandardLending(ii)=StandardLending(ii)+fredData(ind,2);
end
end
end
trace4=  struct(‘x’, {cellstr(datestr(fred_WLCFLL.Data(:,1),’yyyy-mm’))},’y’,StandardLending,’name’, ‘Lending to Banks’,’type’, ‘scatter’);% ,’line’, struct(‘color’, ColorString));

% Treasury Holdings
% U.S. Treasury securities held by the Federal Reserve: All Maturities (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_TREAST = getFredData(‘TREAST’, GRdate1, GRdate2)
trace5= struct(‘x’, {cellstr(datestr(fred_TREAST.Data(:,1),’yyyy-mm’))},’y’,fred_TREAST.Data(:,2),’name’, ‘Treasuries’,’type’, ‘scatter’);% ,’line’, struct(‘color’, ColorString));
% Breakdown of Treasuries
% U.S. Treasury securities held by the Federal Reserve: Maturing in over 10 years (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_TREAS10Y = getFredData(‘TREAS10Y’, GRdate1, GRdate2)
% U.S. Treasury securities held by the Federal Reserve: Maturing in over 5 years to 10 years (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_TREAS5T10 = getFredData(‘TREAS5T10’, GRdate1, GRdate2)
% U.S. Treasury securities held by the Federal Reserve: Maturing in over 1 year to 5 years (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_TREAS1T5 = getFredData(‘TREAS1T5’, GRdate1, GRdate2)
% U.S. Treasury securities held by the Federal Reserve: Maturing in 91 days to 1 year (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_TREAS911Y = getFredData(‘TREAS911Y’, GRdate1, GRdate2)
% U.S. Treasury securities held by the Federal Reserve: Maturing in 16 days to 90 days (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_TREAS1590 = getFredData(‘TREAS1590’, GRdate1, GRdate2)
% U.S. Treasury securities held by the Federal Reserve: Maturing within 15 days (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_TREAS15 = getFredData(‘TREAS15’, GRdate1, GRdate2)
LongTermTreasuries=fred_TREAS10Y.Data(:,2)+fred_TREAS5T10.Data(:,2);
ShortTermTreasuries=fred_TREAS1590.Data(:,2)+fred_TREAS15.Data(:,2)+fred_TREAS911Y.Data(:,2)+fred_TREAS1T5.Data(:,2);

% Miscellany
% Factors Supplying Reserve Balances – Reserve Bank Credit – Other Federal Reserve Assets (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WOFRAL = getFredData(‘WOFRAL’, GRdate1, GRdate2)
% Factors Supplying Reserve Balances – Float (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WOFSRBFL = getFredData(‘WOFSRBFL’, GRdate1, GRdate2)
% Factors Supplying Reserve Balances – Treasury Currency Outstanding (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WTCOL = getFredData(‘WTCOL’, GRdate1, GRdate2)
% Assets – Special Drawing Rights Certificate Account (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WASDRAL = getFredData(‘WASDRAL’, GRdate1, GRdate2)
% Factors Supplying Reserve Balances – Gold Stock (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WOFSRBGSL = getFredData(‘WOFSRBGSL’, GRdate1, GRdate2)
% Assets – Foreign Currency Denominated Assets (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WFCDA = getFredData(‘WFCDA’, GRdate1, GRdate2)
Misc=fred_WOFRAL.Data(:,2)+fred_WOFSRBFL.Data(:,2)+fred_WTCOL.Data(:,2)+fred_WASDRAL.Data(:,2)+fred_WOFSRBGSL.Data(:,2)+fred_WFCDA.Data(:,2);
trace6= struct(‘x’, {cellstr(datestr(fred_WOFRAL.Data(:,1),’yyyy-mm’))},’y’,Misc,’name’, ‘Misc.’,’type’, ‘scatter’);% ,’line’, struct(‘color’, ColorString));

% Much of the Bank Lending actually counts in the ‘Reserve Bank Credit’
% line, but not in the subtotals. (Eg.
% http://research.stlouisfed.org/fred2/release/tables?rid=20&eid=2157&od=2009-02-28#)
% Factors Supplying Reserve Balances – Reserve Bank Credit (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WOFSRBRBC = getFredData(‘WOFSRBRBC’, GRdate1, GRdate2)
%Factors Supplying Reserve Balances – Securities Held Outright (Millions of Dollars, Weekly, As of Wednesday, Not Seasonally Adjusted)
fred_WSHOSHO = getFredData(‘WSHOSHO’, GRdate1, GRdate2)
OtherLending=fred_WOFSRBRBC.Data(:,2)-fred_WSHOSHO.Data(:,2)-fred_WUPSHO.Data(:,2)-fred_WUDSHO.Data(:,2)-fred_WOFSRBFL.Data(:,2)-fred_SWPT.Data(:,2)-StandardLending;
%StandardLending: -fred_WARAL.Data(:,2)-fred_WLCFLL.Data(:,2)-fred_WNPHML1L.Data(:,2)-fred_WNPHML2L.Data(:,2)-fred_WNPHML3L.Data(:,2)-fred_WNPHTALFL.Data(:,2)-fred_WOFRAL.Data(:,2);

trace2a= struct(‘x’, {cellstr(datestr(fred_TREAST.Data(:,1),’yyyy-mm’))},’y’,fred_FEDDT_MBST_ETC.Data(:,2)+fred_SWPT.Data(:,2)+OtherLending+StandardLending+fred_TREAST.Data(:,2)+Misc,’name’, ‘Fed Agency Debt + MBS’,’type’, ‘scatter’,’fill’,’tonexty’);
trace3a= struct(‘x’, {cellstr(datestr(fred_TREAST.Data(:,1),’yyyy-mm’))},’y’,fred_SWPT.Data(:,2)+OtherLending+StandardLending+fred_TREAST.Data(:,2)+Misc,’name’, ‘Liquidity to Key Markets (Currency Swaps)’,’type’, ‘scatter’,’fill’,’tonexty’);
trace4a= struct(‘x’, {cellstr(datestr(fred_TREAST.Data(:,1),’yyyy-mm’))},’y’,OtherLending+StandardLending+fred_TREAST.Data(:,2)+Misc,’name’, ‘Lending to Banks (inc. Bailouts)’,’type’, ‘scatter’,’fill’,’tonexty’);
%trace5a= struct(‘x’, {cellstr(datestr(fred_TREAST.Data(:,1),’yyyy-mm’))},’y’,fred_TREAST.Data(:,2)+Misc,’name’, ‘Treasuries’,’type’, ‘scatter’,’fill’,’tonexty’);
trace5a_1= struct(‘x’, {cellstr(datestr(fred_TREAST.Data(:,1),’yyyy-mm’))},’y’,LongTermTreasuries+ShortTermTreasuries+Misc,’name’, ‘Longer-Term Treasuries’,’type’, ‘scatter’,’fill’,’tonexty’);
trace5a_2= struct(‘x’, {cellstr(datestr(fred_TREAST.Data(:,1),’yyyy-mm’))},’y’,ShortTermTreasuries+Misc,’name’, ‘Short-Term Treasuries’,’type’, ‘scatter’,’fill’,’tonexty’);
trace6a= struct(‘x’, {cellstr(datestr(fred_TREAST.Data(:,1),’yyyy-mm’))},’y’,Misc,’name’, ‘Miscellaneous’,’type’, ‘scatter’,’fill’,’tozeroy’);

data={trace6a,trace5a_2,trace5a_1,trace4a,trace3a,trace2a};
layout = struct(‘title’, ‘US Federal Reserve Balance Sheet in Great Recession’,’showlegend’, true,’width’, 800,’xaxis’, struct(‘domain’, [0, 0.9],’title’,’Year’), …
‘yaxis’, struct(‘title’, ‘Millions of Dollars’,’titlefont’, struct(‘color’, ‘black’),’tickfont’, struct(‘color’, ‘black’),…
‘anchor’, ‘free’,’side’, ‘left’,’position’,0) );
response = plotly(data, struct(‘layout’, layout, ‘filename’, ‘GreatRecession_FedReserveBalanceSheet’, ‘fileopt’, ‘overwrite’));
response.data=data; response.layout=layout;
saveplotlyfig(response, [‘./Graphs/GreatRecession_FedReserveBalanceSheet.pdf’])