Changes in spending
Layered line chart with vertical lines and text overlay
Today we will study the charts in the article The Rich Cut Their Spending. That Has Hurt All the Workers Who Count on It. These charts tell us something very important about how the spending has been cut differently across different classes.
- First stimulus checks - April 17
- States in the process of reopening - May 1
The data for this analysis is taken from Opportunity Labs where they publish their data in this dashboard.
What's important about this data is best summed up by -
One of the things this crisis has made salient is how interdependent our health was, said Michael Stepner, an economist at the University of Toronto. We’re seeing the mirror of that on the economic side.
#hide_output
import pandas as pd
import altair as alt
alt.renderers.set_embed_options(actions=False)
spending_uri
with this - ’https://raw.githubusercontent.com/OpportunityInsights/EconomicTracker/main/data/Affinity%20-%20National%20-%20Daily.csv’
spending_uri = 'https://raw.githubusercontent.com/OpportunityInsights/EconomicTracker/8d9fae46fab3e386a8f4ce798de09a016cbda0f9/data/Affinity%20-%20National%20-%20Daily.csv'
#spending_uri = 'https://raw.githubusercontent.com/Opportunitylab/EconomicTracker/main/data/Affinity%20-%20National%20-%20Weekly.csv' # for latest data
spending = pd.read_csv(spending_uri)
spending.head()
def add_format_date(df):
df['date'] = df['year'].astype(str) + '-' + df['month'].astype(str) + '-' + df['day'].astype(str)
df['date'] = pd.to_datetime(df['date'], format="%Y-%m-%d")
return df
spending = spending.pipe(add_format_date)
spending.head()
Plotting the data -
base=alt.Chart(spending).transform_fold(['spend_all_inchigh', 'spend_all_inclow', 'spend_all_incmiddle']).transform_filter(alt.datum.date > alt.expr.toDate('2020-02-14')).mark_line().encode(
x=alt.X('date:T', title=None, axis=alt.Axis(format="%b%e", tickCount=5, labelOffset=0, tickOffset=0, labelPadding=25, ticks=False)),
#y='spend_all_inchigh:Q',
#x2='date:Q',
y=alt.Y('value:Q', title=None, axis=alt.Axis(format="%", tickCount=10)),
color='key:N'
#detail='date'
).properties(width=900, height=600)
lines={'lines': ['2020-04-15', '2020-05-01'], 'y1': [0,0], 'y2': [-0.4, -0.4]}
lines1={'lines': ['2020-04-15'], 'text': ['First stimulus \n checks received'], 'y': [-0.03]}
lines2={'lines': ['2020-05-01'], 'text': ['Half of states in \n process of reopening'], 'y': [-0.03]}
vert_line = alt.Chart(pd.DataFrame(lines)).mark_rule(strokeDash=[5,5], stroke='grey').encode(
x='lines:T',
y=alt.Y('y1:Q', scale=alt.Scale(zero=False)),
#y2=alt.Y2('y2:Q')
)
text1 = alt.Chart(pd.DataFrame(lines1)).mark_text(lineBreak='\n', dx=-10, align='right').encode(
text = 'text:N',
y = 'y:Q',
x = 'lines:T'
)
text2 = alt.Chart(pd.DataFrame(lines2)).mark_text(lineBreak='\n',dx=10, align='left').encode(
text = 'text:N',
y = 'y:Q',
x = 'lines:T',
)
alt.layer(base, vert_line, text1, text2).configure_view(strokeWidth=0).configure_axis(grid=False).configure_axisX(orient='top', offset=-67)
We can use the same techniques for the vertical lines and text overlay as the chart above in the following charts. Since the idea is similar I will not implement them for all, instead just plot the line charts.
revenue_uri = 'https://raw.githubusercontent.com/Opportunitylab/EconomicTracker/main/data/Womply%20Revenue%20-%20National%20-%20Daily.csv'
revenue = pd.read_csv(revenue_uri)
revenue = revenue.pipe(add_format_date)
revenue.head()
alt.Chart(revenue).mark_line().transform_fold(['revenue_inclow', 'revenue_incmiddle', 'revenue_inchigh']).transform_filter(alt.datum.date > alt.expr.toDate('2020-02-14')).encode(
x='date:T',
y= 'value:Q',
color= 'key:N'
)
earning_uri = 'https://raw.githubusercontent.com/OpportunityInsights/EconomicTracker/5f914ee4e71f56a33857b63e0bd07d71bc31e847/data/Low%20Inc%20Earnings%20Small%20Businesses%20-%20National%20-%20Daily.csv'
earning = pd.read_csv(earning_uri)
earning = earning.pipe(add_format_date)
earning.head()
alt.Chart(earning).mark_line().transform_fold(['pay', 'pay_inclow', 'pay_incmiddle', 'pay_inchigh']).encode(
x='date:T',
y= 'value:Q',
color= 'key:N'
)
#employment_uri = 'https://raw.githubusercontent.com/Opportunitylab/EconomicTracker/main/data/Low%20Inc%20Emp%20Small%20Businesses%20-%20National%20-%20Daily.csv' # original file
employment_uri = 'https://raw.githubusercontent.com/OpportunityInsights/EconomicTracker/ba8c0096efb873d90f10cd720576c4ec5e6fc42e/data/Low%20Inc%20Emp%20Small%20Businesses%20-%20National%20-%20Daily.csv'
employment = pd.read_csv(employment_uri)
employment = employment.pipe(add_format_date)
employment.head()
alt.Chart(employment).mark_line().transform_fold(['emp_inclow', 'emp_incmiddle', 'emp_inchigh']).encode(
x='date:T',
y= 'value:Q',
color= 'key:N'
)