awacke1 commited on
Commit
e906845
Β·
1 Parent(s): 5cbf324

Update backup-app.py

Browse files
Files changed (1) hide show
  1. backup-app.py +266 -174
backup-app.py CHANGED
@@ -2,7 +2,6 @@ import streamlit as st
2
  import pandas as pd
3
  import altair as alt
4
 
5
- # Define list of largest hospitals with latitude and longitude
6
  largest_hospitals = [
7
  {
8
  'name': 'Florida Hospital Orlando',
@@ -10,8 +9,8 @@ largest_hospitals = [
10
  'state': 'FL',
11
  'zip_code': '32803',
12
  'bed_count': 2411,
13
- 'lat': 28.562229,
14
- 'lng': -81.362976
15
  },
16
  {
17
  'name': 'Cleveland Clinic',
@@ -19,8 +18,8 @@ largest_hospitals = [
19
  'state': 'OH',
20
  'zip_code': '44195',
21
  'bed_count': 1730,
22
- 'lat': 41.501669,
23
- 'lng': -81.621275
24
  },
25
  {
26
  'name': 'Mayo Clinic',
@@ -28,8 +27,8 @@ largest_hospitals = [
28
  'state': 'MN',
29
  'zip_code': '55905',
30
  'bed_count': 1372,
31
- 'lat': 44.020634,
32
- 'lng': -92.463476
33
  },
34
  {
35
  'name': 'NewYork-Presbyterian Hospital-Columbia and Cornell',
@@ -37,8 +36,8 @@ largest_hospitals = [
37
  'state': 'NY',
38
  'zip_code': '10032',
39
  'bed_count': 2332,
40
- 'lat': 40.840886,
41
- 'lng': -73.942184
42
  },
43
  {
44
  'name': 'UCHealth University of Colorado Hospital',
@@ -46,8 +45,8 @@ largest_hospitals = [
46
  'state': 'CO',
47
  'zip_code': '80045',
48
  'bed_count': 672,
49
- 'lat': 39.742401,
50
- 'lng': -104.834694
51
  },
52
  {
53
  'name': 'Houston Methodist Hospital',
@@ -55,8 +54,8 @@ largest_hospitals = [
55
  'state': 'TX',
56
  'zip_code': '77030',
57
  'bed_count': 1063,
58
- 'lat': 29.710292,
59
- 'lng': -95.399262
60
  },
61
  {
62
  'name': 'Johns Hopkins Hospital',
@@ -64,8 +63,8 @@ largest_hospitals = [
64
  'state': 'MD',
65
  'zip_code': '21287',
66
  'bed_count': 1293,
67
- 'lat': 39.297082,
68
- 'lng': -76.590726
69
  },
70
  {
71
  'name': 'Massachusetts General Hospital',
@@ -73,8 +72,8 @@ largest_hospitals = [
73
  'state': 'MA',
74
  'zip_code': '02114',
75
  'bed_count': 1032,
76
- 'lat': 42.363371,
77
- 'lng': -71.068635
78
  },
79
  {
80
  'name': 'University of Michigan Hospitals-Michigan Medicine',
@@ -82,8 +81,8 @@ largest_hospitals = [
82
  'state': 'MI',
83
  'zip_code': '48109',
84
  'bed_count': 1145,
85
- 'lat': 42.282531,
86
- 'lng': -83.728376
87
  },
88
  {
89
  'name': 'Mount Sinai Hospital',
@@ -91,54 +90,59 @@ largest_hospitals = [
91
  'state': 'NY',
92
  'zip_code': '10029',
93
  'bed_count': 1168,
94
- 'lat': 40.789866,
95
- 'lng': -73.952348
96
  }
97
  ]
98
 
99
  largest_hospitals_df = pd.DataFrame(largest_hospitals)
100
 
101
- # Define chart functions
102
- def stacked_bar_chart():
103
  chart = alt.Chart(largest_hospitals_df).mark_bar().encode(
104
  y=alt.Y('state:N', sort='-x'),
105
  x=alt.X('bed_count:Q', stack='normalize'),
106
- color=alt.Color('state:N'),
107
- tooltip=['state', 'bed_count']
108
  ).properties(
109
  width=700,
110
  height=500,
111
- title='Largest Hospitals by State (Stacked Bar Chart)'
 
 
112
  )
113
- st.altair_chart(chart)
 
 
 
114
 
115
  def bump_chart():
116
  chart = alt.Chart(largest_hospitals_df).transform_joinaggregate(
117
- max_bed_count='max(bed_count)',
118
- ).transform_window(
119
- rank='rank(max_bed_count)',
120
- sort=[alt.SortField('max_bed_count', order='descending')]
121
  ).transform_filter(
122
- alt.datum.rank <= 10
 
 
 
123
  ).mark_line().encode(
124
- y=alt.Y('name:N', sort=alt.EncodingSortField('bed_count', order='descending')),
125
- x=alt.X('bed_count:Q'),
126
- color=alt.Color('state:N'),
127
- tooltip=['name', 'bed_count', 'state']
128
  ).properties(
129
  width=700,
130
  height=500,
131
- title='Largest Hospitals by Bed Count (Bump Chart)'
132
  )
133
  st.altair_chart(chart)
134
 
135
  def radial_chart():
136
- chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
137
- x='sum(bed_count)',
138
- y='state',
139
- size='sum(bed_count)',
140
- color='state',
141
- tooltip=['state', 'bed_count']
142
  ).properties(
143
  width=700,
144
  height=500,
@@ -147,82 +151,109 @@ def radial_chart():
147
  st.altair_chart(chart)
148
 
149
  def trellis_area_sort_chart():
150
- chart = alt.Chart(largest_hospitals_df).mark_area().encode(
151
- x=alt.X('year:O', title='Year'),
152
- y=alt.Y('bed_count:Q', title='Bed Count'),
153
- color=alt.Color('state:N', legend=alt.Legend(title='State')),
154
- row=alt.Row('name:N', sort='-x', title='Hospital')
155
- ).transform_calculate(
156
- year='substring(zip_code, 0, 2) + "00"'
157
  ).properties(
158
- width=700,
159
- height=500,
160
- title='Largest Hospitals by Year (Trellis Area Sort Chart)'
 
 
 
 
161
  )
162
  st.altair_chart(chart)
163
 
164
  def wind_vector_map():
165
- airports_df = pd.read_csv('https://raw.githubusercontent.com/hvo/datasets/master/nyc_airports.csv')
166
- airports = alt.Chart(airports_df).mark_circle(size=100).encode(
167
- longitude='lon:Q',
168
- latitude='lat:Q',
169
- tooltip=['name', 'city', 'state']
170
- )
171
-
172
- wind_df = pd.read_csv('https://raw.githubusercontent.com/vega/vega/master/docs/data/wind.csv')
173
- wind = alt.Chart(wind_df).mark_line().encode(
174
- x='u:Q',
175
- y='v:Q',
176
- color=alt.Color('speed:Q', scale=alt.Scale(scheme='inferno')),
177
- size='speed:Q',
178
- tooltip=['u', 'v', 'speed']
179
- )
180
 
181
- chart = alt.layer(
182
- alt.themes.dark(),
183
- alt.repeat(row=range(4), column=range(4), layer=0, data=largest_hospitals_df),
184
- airports,
185
- wind.transform_filter(
186
- alt.datum.zip_code == str(largest_hospitals_df.iloc[0]['zip_code'])
187
- ).transform_calculate(
188
- azimuth='atan2(v, u)',
189
- speed='sqrt(u * u + v * v)',
190
- dx='cos(azimuth * PI/180) * speed',
191
- dy='sin(azimuth * PI/180) * speed'
192
- ).mark_arrow().encode(
193
- longitude='lng:Q',
194
- latitude='lat:Q',
195
- angle='azimuth:Q',
196
- size=alt.Size('speed:Q', scale=alt.Scale(range=[0, 50])),
197
- tooltip=['u', 'v', 'speed', 'azimuth']
198
- )
199
  ).properties(
200
  width=700,
201
- height=500,
202
- title='Wind Vectors and Airports in New York City'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  )
204
- st.altair_chart(chart)
 
205
 
206
  def table_bubble_plot():
207
  chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
208
  x=alt.X('bed_count:Q', title='Bed Count'),
209
- y=alt.Y('state:N', sort='-x', title='State'),
210
  size=alt.Size('bed_count:Q', title='Bed Count'),
211
- color=alt.Color('state:N'),
212
- tooltip=['name', 'bed_count', 'city', 'state']
213
  ).properties(
214
  width=700,
215
  height=500,
216
- title='Largest Hospitals in the US (Table Bubble Plot)'
217
  )
218
  st.altair_chart(chart)
219
 
220
  def locations_of_us_airports():
221
- airports_df = pd.read_csv('https://raw.githubusercontent.com/hvo/datasets/master/nyc_airports.csv')
222
- chart = alt.Chart(airports_df).mark_circle(size=100).encode(
223
- longitude='lon:Q',
224
- latitude='lat:Q',
225
- tooltip=['name', 'city', 'state']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  ).properties(
227
  width=700,
228
  height=500,
@@ -231,135 +262,195 @@ def locations_of_us_airports():
231
  st.altair_chart(chart)
232
 
233
  def connections_among_us_airports_interactive():
234
- airports_df = pd.read_csv('https://raw.githubusercontent.com/hvo/datasets/master/nyc_airports.csv')
235
- routes_df = pd.read_csv('https://raw.githubusercontent.com/vega/vega/master/docs/data/flights-2k.csv')
236
- source = alt.selection_multi(fields=['origin'])
237
- chart = alt.Chart(routes_df).mark_geoshape(stroke='white', strokeWidth=0.5).encode(
238
- color=alt.condition(source, alt.value('red'), alt.value('lightgray')),
239
- tooltip=['origin', 'destination', 'count']
240
- ).transform_lookup(
241
- lookup='id',
242
- from_=alt.LookupData(airports_df, 'id', ['name', 'city', 'state'])
243
- ).project(
244
- type='albersUsa'
 
245
  ).properties(
246
  width=700,
247
- height=500,
248
- title='Connections Among US Airports (Interactive)'
249
- )
250
- points = alt.Chart(airports_df).mark_circle(size=100).encode(
251
- longitude='lon:Q',
252
- latitude='lat:Q',
253
- tooltip=['name', 'city', 'state', 'id']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
  ).transform_filter(
255
- source
 
 
 
256
  )
257
- st.altair_chart(chart + points)
 
 
 
 
 
 
258
 
259
  def one_dot_per_zipcode():
260
- chart = alt.Chart(largest_hospitals_df).mark_circle(size=50).encode(
261
  longitude='lng:Q',
262
  latitude='lat:Q',
263
- color=alt.Color('state:N', scale=alt.Scale(scheme='category10')),
264
- tooltip=['name', 'city', 'state', 'bed_count']
 
265
  ).properties(
266
  width=700,
267
  height=500,
268
- title='Largest Hospitals in the US (One Dot per Zip Code)'
269
  )
270
  st.altair_chart(chart)
271
 
272
  def isotype_visualization_with_emoji():
273
- chart = alt.Chart(largest_hospitals_df).mark_image(width=50, height=50).encode(
274
- x=alt.X('bed_count:Q', axis=None),
275
- y=alt.Y('state:N', sort='-x', axis=None),
276
- url='https://raw.githubusercontent.com/twitter/twemoji/v13.0.1/assets/svg/1f628.svg',
277
- tooltip=['name', 'bed_count']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278
  ).properties(
279
  width=700,
280
  height=500,
281
- title='Largest Hospitals in the US (Isotype Visualization with Emoji)'
282
  )
283
  st.altair_chart(chart)
284
 
285
  def binned_heatmap():
286
  chart = alt.Chart(largest_hospitals_df).mark_rect().encode(
287
  x=alt.X('bed_count:Q', bin=True),
288
- y=alt.Y('state:N', sort='-x'),
289
- color=alt.Color('count()'),
290
- tooltip=['state', 'bed_count']
291
  ).properties(
292
  width=700,
293
  height=500,
294
- title='Largest Hospitals by State (Binned Heatmap)'
295
  )
296
  st.altair_chart(chart)
297
 
298
  def facetted_scatterplot_with_marginal_histograms():
299
- chart = alt.Chart(largest_hospitals_df).mark_circle(size=50).encode(
 
 
 
 
 
 
 
 
 
300
  x=alt.X('bed_count:Q', title='Bed Count'),
301
- y=alt.Y('state:N', sort='-x', title='State'),
302
  color=alt.Color('state:N'),
303
- tooltip=['name', 'bed_count', 'city', 'state']
304
- ).properties(
305
- width=700,
306
- height=500,
307
- title='Largest Hospitals in the US (Facetted Scatterplot with Marginal Histograms)'
308
- ).facet(
309
- column=alt.Column('state:N', sort='-y', title='State'),
310
- spacing={'column': 30}
311
  )
312
- histogram_x = largest_hospitals_df[['bed_count']].reset_index().rename(columns={'bed_count': 'value'})
313
- histogram_x['variable'] = 'bed_count'
314
- histogram_y = largest_hospitals_df[['state']].reset_index().rename(columns={'state': 'value'})
315
- histogram_y['variable'] = 'state'
316
- chart_x = alt.Chart(histogram_x).mark_bar().encode(
317
- x=alt.X('value:Q', bin=True),
318
- y=alt.Y('count()'),
319
- color=alt.Color('variable:N', scale=alt.Scale(range=['#675193', '#ca8861'])),
320
- tooltip=['value']
321
  ).properties(
322
- width=700,
 
323
  height=100
324
  )
325
- chart_y = alt.Chart(histogram_y).mark_bar().encode(
326
- y=alt.Y('value:N', sort='-x'),
327
- x=alt.X('count()'),
328
- color=alt.Color('variable:N', scale=alt.Scale(range=['#675193', '#ca8861'])),
329
- tooltip=['value']
330
  ).properties(
 
331
  width=100,
332
  height=500
333
  )
334
- st.altair_chart(chart | chart_x, use_container_width=True)
335
- st.altair_chart(chart_y, use_container_width=True)
336
 
 
 
 
 
 
 
 
 
 
 
 
337
  def ridgeline_plot():
338
- chart = alt.Chart(largest_hospitals_df).transform_joinaggregate(
339
- count='count()',
340
- groupby=['state']
341
- ).transform_window(
342
- rank='rank(count)',
343
- sort=[alt.SortField('count', order='descending')]
344
- ).transform_filter(
345
- alt.datum.rank <= 5
346
- ).transform_density(
347
- density='bed_count',
348
  as_=['bed_count', 'density'],
349
  extent=[0, 3000],
 
350
  groupby=['state']
351
- ).mark_area().encode(
352
- y=alt.Y('state:N', sort='-x'),
 
353
  x=alt.X('bed_count:Q', title='Bed Count'),
 
354
  color=alt.Color('state:N'),
355
- row=alt.Row('rank:O', sort='descending', title=None)
356
  ).properties(
 
357
  width=700,
358
- height=500,
359
- title='Largest Hospitals by State (Ridgeline Plot)'
360
  )
 
361
  st.altair_chart(chart)
362
 
 
363
  def create_sidebar():
364
  chart_functions = {
365
  'Stacked Bar Chart with Text Overlay': stacked_bar_chart,
@@ -403,3 +494,4 @@ def emoji(chart_name):
403
  return emojis.get(chart_name, '')
404
 
405
  create_sidebar()
 
 
2
  import pandas as pd
3
  import altair as alt
4
 
 
5
  largest_hospitals = [
6
  {
7
  'name': 'Florida Hospital Orlando',
 
9
  'state': 'FL',
10
  'zip_code': '32803',
11
  'bed_count': 2411,
12
+ 'lat': 28.555149,
13
+ 'lng': -81.362244
14
  },
15
  {
16
  'name': 'Cleveland Clinic',
 
18
  'state': 'OH',
19
  'zip_code': '44195',
20
  'bed_count': 1730,
21
+ 'lat': 41.501642,
22
+ 'lng': -81.621223
23
  },
24
  {
25
  'name': 'Mayo Clinic',
 
27
  'state': 'MN',
28
  'zip_code': '55905',
29
  'bed_count': 1372,
30
+ 'lat': 44.019126,
31
+ 'lng': -92.463362
32
  },
33
  {
34
  'name': 'NewYork-Presbyterian Hospital-Columbia and Cornell',
 
36
  'state': 'NY',
37
  'zip_code': '10032',
38
  'bed_count': 2332,
39
+ 'lat': 40.841708,
40
+ 'lng': -73.942635
41
  },
42
  {
43
  'name': 'UCHealth University of Colorado Hospital',
 
45
  'state': 'CO',
46
  'zip_code': '80045',
47
  'bed_count': 672,
48
+ 'lat': 39.743943,
49
+ 'lng': -104.834322
50
  },
51
  {
52
  'name': 'Houston Methodist Hospital',
 
54
  'state': 'TX',
55
  'zip_code': '77030',
56
  'bed_count': 1063,
57
+ 'lat': 29.710773,
58
+ 'lng': -95.399676
59
  },
60
  {
61
  'name': 'Johns Hopkins Hospital',
 
63
  'state': 'MD',
64
  'zip_code': '21287',
65
  'bed_count': 1293,
66
+ 'lat': 39.296154,
67
+ 'lng': -76.591972
68
  },
69
  {
70
  'name': 'Massachusetts General Hospital',
 
72
  'state': 'MA',
73
  'zip_code': '02114',
74
  'bed_count': 1032,
75
+ 'lat': 42.362251,
76
+ 'lng': -71.069405
77
  },
78
  {
79
  'name': 'University of Michigan Hospitals-Michigan Medicine',
 
81
  'state': 'MI',
82
  'zip_code': '48109',
83
  'bed_count': 1145,
84
+ 'lat': 42.285932,
85
+ 'lng': -83.730833
86
  },
87
  {
88
  'name': 'Mount Sinai Hospital',
 
90
  'state': 'NY',
91
  'zip_code': '10029',
92
  'bed_count': 1168,
93
+ 'lat': 40.788127,
94
+ 'lng': -73.952826
95
  }
96
  ]
97
 
98
  largest_hospitals_df = pd.DataFrame(largest_hospitals)
99
 
100
+ def stacked_bar_chart_with_text_overlay():
 
101
  chart = alt.Chart(largest_hospitals_df).mark_bar().encode(
102
  y=alt.Y('state:N', sort='-x'),
103
  x=alt.X('bed_count:Q', stack='normalize'),
104
+ color=alt.Color('name:N'),
105
+ tooltip=['name', 'city', 'state', 'bed_count']
106
  ).properties(
107
  width=700,
108
  height=500,
109
+ title='Largest Hospitals by State (Stacked Bar Chart with Text Overlay)'
110
+ ).configure_axisX(
111
+ labelAngle=-45
112
  )
113
+ text = chart.mark_text(align='left', baseline='middle', dx=3).encode(
114
+ text=alt.Text('bed_count:Q', format='.1f')
115
+ )
116
+ st.altair_chart(chart + text)
117
 
118
  def bump_chart():
119
  chart = alt.Chart(largest_hospitals_df).transform_joinaggregate(
120
+ rank='rank(bed_count)',
121
+ groupby=['state']
 
 
122
  ).transform_filter(
123
+ alt.datum.rank <= 3
124
+ ).transform_window(
125
+ y='row_number()',
126
+ sort=[alt.SortField('bed_count', order='descending')]
127
  ).mark_line().encode(
128
+ x=alt.X('bed_count:Q', title='Bed Count'),
129
+ y=alt.Y('y:O', axis=None),
130
+ color=alt.Color('name:N'),
131
+ tooltip=['name', 'city', 'state', 'bed_count']
132
  ).properties(
133
  width=700,
134
  height=500,
135
+ title='Largest Hospitals by State (Bump Chart)'
136
  )
137
  st.altair_chart(chart)
138
 
139
  def radial_chart():
140
+ chart = alt.Chart(largest_hospitals_df).mark_bar().encode(
141
+ x=alt.X('count()', title='Count'),
142
+ y=alt.Y('state:N', sort='-x'),
143
+ color=alt.Color('bed_count:Q', legend=None),
144
+ column=alt.Column('bed_count:Q', bin=alt.Bin(maxbins=10)),
145
+ tooltip=['name', 'city', 'state', 'bed_count']
146
  ).properties(
147
  width=700,
148
  height=500,
 
151
  st.altair_chart(chart)
152
 
153
  def trellis_area_sort_chart():
154
+ chart = alt.Chart(largest_hospitals_df).mark_area(opacity=0.8).encode(
155
+ x=alt.X('yearmonth(date):T', title='Date'),
156
+ y=alt.Y('sum(revenue):Q', stack='center', axis=None),
157
+ color=alt.Color('product:N', scale=alt.Scale(scheme='category10')),
158
+ row=alt.Row('market:N', header=alt.Header(title='Market')),
159
+ column=alt.Column('product:N', header=alt.Header(title='Product')),
160
+ tooltip=[alt.Tooltip('product:N'), alt.Tooltip('revenue:Q', format='$,.0f')]
161
  ).properties(
162
+ width=300,
163
+ height=200,
164
+ title='Trellis Area Sort Chart'
165
+ ).configure_facet(
166
+ spacing=0
167
+ ).configure_view(
168
+ stroke=None
169
  )
170
  st.altair_chart(chart)
171
 
172
  def wind_vector_map():
173
+ source = pd.DataFrame({
174
+ 'lat': largest_hospitals_df['lat'],
175
+ 'lon': largest_hospitals_df['lng'],
176
+ 'u': [10, 20, 30, 40, 50, -10, -20, -30, -40, -50],
177
+ 'v': [-10, -20, -30, -40, -50, 10, 20, 30, 40, 50],
178
+ 'names': largest_hospitals_df['name']
179
+ })
180
+ max_speed = 60
 
 
 
 
 
 
 
181
 
182
+ # Create a layer of the world map
183
+ background = alt.Chart(
184
+ data=topo_feature('world-110m')
185
+ ).mark_geoshape(
186
+ fill='white',
187
+ stroke='lightgray'
 
 
 
 
 
 
 
 
 
 
 
 
188
  ).properties(
189
  width=700,
190
+ height=400
191
+ ).project('naturalEarth1')
192
+
193
+ # Add the wind vectors as arrows
194
+ vectors = background.mark_arrow(
195
+ length=300,
196
+ stroke='black',
197
+ strokeWidth=0.5
198
+ ).encode(
199
+ longitude='lon:Q',
200
+ latitude='lat:Q',
201
+ angle=alt.Angle('atan2(v, u):Q'),
202
+ size=alt.Size(alt.Color('length:Q', legend=None), scale=alt.Scale(range=[0, 0.08]), title='Wind speed'),
203
+ opacity=alt.Opacity(alt.Color('length:Q', legend=None), scale=alt.Scale(range=[0, 1]), title='Wind speed'),
204
+ tooltip=['names:N', alt.Tooltip('length:Q', format='.1f')]
205
+ ).transform_calculate(
206
+ # Cartographic rotation for arrows
207
+ angle=calc_wind_angle('u', 'v'),
208
+ # Vector length
209
+ length=calc_wind_speed('u', 'v'),
210
+ # Limit vector length
211
+ length=alt.datum.length > max_speed ? max_speed : alt.datum.length
212
  )
213
+
214
+ st.altair_chart(background + vectors)
215
 
216
  def table_bubble_plot():
217
  chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
218
  x=alt.X('bed_count:Q', title='Bed Count'),
219
+ y=alt.Y('state:N', sort='-x'),
220
  size=alt.Size('bed_count:Q', title='Bed Count'),
221
+ color=alt.Color('bed_count:Q', legend=None),
222
+ tooltip=['name', 'city', 'state', 'bed_count']
223
  ).properties(
224
  width=700,
225
  height=500,
226
+ title='Largest Hospitals by State (Table Bubble Plot)'
227
  )
228
  st.altair_chart(chart)
229
 
230
  def locations_of_us_airports():
231
+ airports = data.airports.url
232
+
233
+ states = alt.topo_feature(data.us_10m.url, 'states')
234
+ lookup = {'New York City': 'New York', 'Chicago': 'Illinois', 'Los Angeles': 'California', 'San Francisco': 'California', 'Houston': 'Texas'}
235
+
236
+ chart = alt.Chart(states).mark_geoshape(
237
+ fill='lightgray',
238
+ stroke='white'
239
+ ).encode(
240
+ color=alt.Color('count()', scale=alt.Scale(scheme='yelloworangered')),
241
+ tooltip=[alt.Tooltip('state:N'), alt.Tooltip('count():Q')]
242
+ ).transform_lookup(
243
+ lookup='state',
244
+ from_=alt.LookupData(airports, 'state', ['latitude', 'longitude'])
245
+ ).transform_fold(
246
+ ['latitude', 'longitude'],
247
+ as_=['key', 'value']
248
+ ).transform_filter(
249
+ (alt.datum.value[0] != 'NaN') & (alt.datum.value[1] != 'NaN')
250
+ ).mark_circle(
251
+ size=10
252
+ ).encode(
253
+ longitude='value:Q',
254
+ latitude='key:Q',
255
+ color=alt.Color('count()', scale=alt.Scale(scheme='yelloworangered')),
256
+ tooltip=[alt.Tooltip('state:N'), alt.Tooltip('count():Q')]
257
  ).properties(
258
  width=700,
259
  height=500,
 
262
  st.altair_chart(chart)
263
 
264
  def connections_among_us_airports_interactive():
265
+ airports = data.airports.url
266
+ routes = data.routes.url
267
+
268
+ states = alt.topo_feature(data.us_10m.url, 'states')
269
+
270
+ source = pd.read_json(airports)
271
+ lookup = {'New York City': 'New York', 'Chicago': 'Illinois', 'Los Angeles': 'California', 'San Francisco': 'California', 'Houston': 'Texas'}
272
+ source['state'] = source['state'].apply(lambda x: lookup[x] if x in lookup.keys() else x)
273
+
274
+ base = alt.Chart(states).mark_geoshape(
275
+ fill='lightgray',
276
+ stroke='white'
277
  ).properties(
278
  width=700,
279
+ height=400
280
+ ).project('albersUsa')
281
+
282
+ airports = base.mark_circle(size=10).encode(
283
+ longitude='longitude:Q',
284
+ latitude='latitude:Q',
285
+ tooltip=['name:N', 'city:N', 'state:N', 'country:N']
286
+ ).transform_lookup(
287
+ lookup='iata',
288
+ from_=alt.LookupData(airports, 'iata', ['name', 'city', 'state', 'country', 'latitude', 'longitude'])
289
+ ).properties(title='US Airports')
290
+
291
+ routes = base.mark_geoshape(
292
+ stroke='black',
293
+ strokeWidth=0.1
294
+ ).encode(
295
+ longitude='start_lon:Q',
296
+ latitude='start_lat:Q',
297
+ longitude2='end_lon:Q',
298
+ latitude2='end_lat:Q'
299
+ ).transform_lookup(
300
+ lookup='start',
301
+ from_=alt.LookupData(source, 'iata', ['state', 'latitude', 'longitude']),
302
+ as_=['start_state', 'start_lat', 'start_lon']
303
+ ).transform_lookup(
304
+ lookup='end',
305
+ from_=alt.LookupData(source, 'iata', ['state', 'latitude', 'longitude']),
306
+ as_=['end_state', 'end_lat', 'end_lon']
307
+ ).transform_filter(
308
+ (alt.datum.start_lat != None) & (alt.datum.start_lon != None) & (alt.datum.end_lat != None) & (alt.datum.end_lon != None)
309
+ ).transform_aggregate(
310
+ count='count()',
311
+ groupby=['start', 'start_state', 'end', 'end_state']
312
  ).transform_filter(
313
+ (alt.datum['count'] > 10)
314
+ ).transform_calculate(
315
+ start_lon=-alt.datum.start_lon,
316
+ end_lon=-alt.datum.end_lon
317
  )
318
+
319
+ chart = (base + routes + airports).configure_view(
320
+ width=800,
321
+ height=500,
322
+ stroke=None
323
+ )
324
+ st.altair_chart(chart)
325
 
326
  def one_dot_per_zipcode():
327
+ chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
328
  longitude='lng:Q',
329
  latitude='lat:Q',
330
+ size=alt.Size('bed_count:Q', title='Bed Count'),
331
+ color=alt.Color('bed_count:Q', legend=None),
332
+ tooltip=['name', 'city', 'state', 'zip_code', 'bed_count']
333
  ).properties(
334
  width=700,
335
  height=500,
336
+ title='One Dot Per Zipcode'
337
  )
338
  st.altair_chart(chart)
339
 
340
  def isotype_visualization_with_emoji():
341
+ chart = alt.Chart(largest_hospitals_df).mark_point().encode(
342
+ x=alt.X('bed_count:Q', title='Bed Count'),
343
+ y=alt.Y('state:N', sort='-x'),
344
+ color=alt.Color('state:N'),
345
+ shape=alt.Shape('state:N'),
346
+ tooltip=['name', 'city', 'state', 'zip_code', 'bed_count']
347
+ )
348
+
349
+ shape_lookup = {'CO': 'πŸ₯', 'FL': 'πŸ₯', 'MA': 'πŸ₯', 'MD': 'πŸ₯', 'MI': 'πŸ₯', 'MN': 'πŸ₯', 'NY': 'πŸ₯', 'OH': 'πŸ₯', 'TX': 'πŸ₯'}
350
+ chart = chart.transform_calculate(
351
+ shape=f'"{shape_lookup}"[datum.state]'
352
+ )
353
+
354
+ chart = chart.mark_text(
355
+ align='center',
356
+ baseline='middle',
357
+ size=30,
358
+ font='Segoe UI Emoji',
359
+ dx=0,
360
+ dy=0,
361
+ ).encode(
362
+ text='shape:N'
363
  ).properties(
364
  width=700,
365
  height=500,
366
+ title='Isotype Visualization with Emoji'
367
  )
368
  st.altair_chart(chart)
369
 
370
  def binned_heatmap():
371
  chart = alt.Chart(largest_hospitals_df).mark_rect().encode(
372
  x=alt.X('bed_count:Q', bin=True),
373
+ y=alt.Y('state:N'),
374
+ color=alt.Color('count()', scale=alt.Scale(scheme='yelloworangered')),
375
+ tooltip=[alt.Tooltip('state:N'), alt.Tooltip('count():Q')]
376
  ).properties(
377
  width=700,
378
  height=500,
379
+ title='Binned Heatmap'
380
  )
381
  st.altair_chart(chart)
382
 
383
  def facetted_scatterplot_with_marginal_histograms():
384
+ brush = alt.selection(type='interval', encodings=['x'])
385
+
386
+ base = alt.Chart(largest_hospitals_df).transform_filter(
387
+ brush
388
+ ).properties(
389
+ width=500,
390
+ height=500
391
+ )
392
+
393
+ points = base.mark_point().encode(
394
  x=alt.X('bed_count:Q', title='Bed Count'),
395
+ y=alt.Y('state:N', sort='-x', title=None),
396
  color=alt.Color('state:N'),
397
+ tooltip=['name', 'city', 'state', 'zip_code', 'bed_count']
 
 
 
 
 
 
 
398
  )
399
+
400
+ top_hist = base.mark_bar().encode(
401
+ x=alt.X('bed_count:Q', title='Bed Count'),
402
+ y=alt.Y('count()', title='Number of Hospitals'),
403
+ color=alt.condition(brush, alt.ColorValue('gray'), alt.ColorValue('lightgray')),
 
 
 
 
404
  ).properties(
405
+ title='Bed Count Distribution',
406
+ width=500,
407
  height=100
408
  )
409
+
410
+ right_hist = base.mark_bar().encode(
411
+ y=alt.X('state:N', title='State'),
412
+ x=alt.X('count()', title='Number of Hospitals'),
413
+ color=alt.condition(brush, alt.ColorValue('gray'), alt.ColorValue('lightgray')),
414
  ).properties(
415
+ title='Hospital Count by State',
416
  width=100,
417
  height=500
418
  )
 
 
419
 
420
+ chart = ((points | top_hist) & right_hist).add_selection(
421
+ brush
422
+ ).configure_view(
423
+ stroke=None
424
+ ).properties(
425
+ title='Facetted Scatterplot with Marginal Histograms',
426
+ width=700,
427
+ height=500
428
+ )
429
+ st.altair_chart(chart)
430
+
431
  def ridgeline_plot():
432
+ base = alt.Chart(largest_hospitals_df).transform_density(
433
+ 'bed_count',
 
 
 
 
 
 
 
 
434
  as_=['bed_count', 'density'],
435
  extent=[0, 3000],
436
+ bandwidth=50,
437
  groupby=['state']
438
+ )
439
+
440
+ chart = base.mark_area().encode(
441
  x=alt.X('bed_count:Q', title='Bed Count'),
442
+ y=alt.Y('state:N', sort='-x', title=None),
443
  color=alt.Color('state:N'),
444
+ opacity=alt.Opacity('density:Q', legend=None, scale=alt.Scale(range=[0.3, 1]))
445
  ).properties(
446
+ title='Ridgeline Plot',
447
  width=700,
448
+ height=500
 
449
  )
450
+
451
  st.altair_chart(chart)
452
 
453
+
454
  def create_sidebar():
455
  chart_functions = {
456
  'Stacked Bar Chart with Text Overlay': stacked_bar_chart,
 
494
  return emojis.get(chart_name, '')
495
 
496
  create_sidebar()
497
+