counts1 = h2b.groupby('SOC_NAME').size().reset_index(name='count')sorted_counts1 = counts1.sort_values(by='count', ascending=False)top_ten = sorted_counts1.head(10) #Top ten SOC_NAME and countsfiltered_h2b = h2b[h2b['SOC_NAME'].isin(top_ten['SOC_NAME'])] #Dataset with only top ten SOC_NAMEcounts = filtered_h2b.groupby('EMPLOYER_STATE').size().reset_index(name='count')sorted_counts = counts.sort_values(by='count', ascending=False)top_20 = sorted_counts.head(20)final_filtered_h2b = filtered_h2b[filtered_h2b['EMPLOYER_STATE'].isin(top_20['EMPLOYER_STATE'])] #Dataset with only top ten SOC_NAME & top 20 statesyear_state_soc_counts = final_filtered_h2b.groupby(['YEAR', 'EMPLOYER_STATE', 'SOC_NAME'])['CASE_STATUS'].count()top_soc_names = year_state_soc_counts.groupby(['YEAR', 'EMPLOYER_STATE']).nlargest(30).reset_index(level=[0, 1], drop=True)top_soc_names = top_soc_names.to_frame(name='H2B_Visa_Num').reset_index()
Code
area=(alt.Chart(top_soc_names ).mark_area(opacity=0.6).encode( x=alt.X('YEAR:O', axis=alt.Axis(labelAngle=0 )),y='sum(H2B_Visa_Num):Q' ,color=alt.Color('SOC_NAME:N', scale=alt.Scale(scheme='category20c'), legend=alt.Legend(title='Job Category')), tooltip=[alt.Tooltip('SOC_NAME:N', title="H-2B Job Types"),alt.Tooltip('YEAR:O', title="Year" ),alt.Tooltip('sum(H2B_Visa_Num):Q', title="Summary of number of H-2B Issued" )]).properties(width=500,height=400 ))area.title ="H-2B Visa Job Category Trend, 2011-2019"area.encoding.x.title ='Year'area.encoding.y.title ='Number of H-2B Visa Issued'bar=(alt.Chart(top_soc_names ).mark_bar().encode( y='sum(H2B_Visa_Num):Q', x=alt.X('EMPLOYER_STATE:N', axis=alt.Axis(labelAngle=0), sort=alt.EncodingSortField(field='H2B_Visa_Num',order='descending') ),color=alt.Color('SOC_NAME:N', scale=alt.Scale(scheme='category20c') ),tooltip=[alt.Tooltip('SOC_NAME:N', title="H-2B Job Types"),alt.Tooltip('YEAR:O', title="Year"),alt.Tooltip('H2B_Visa_Num:Q', title="Number of H-2B Issued")])).properties(width=500, height=400)bar.title ="H-2B Visa by State, 2011-2019"bar.encoding.x.title ='State'bar.encoding.y.title ='Number of H-2B Visa Issued'area|bar# Figure 3: Left: H-2B program job category statstics by year. Right: H-2B program job category statistics by state. Both plots include tooltips