File size: 15,800 Bytes
ff4b330
 
 
 
 
 
 
 
 
 
 
e906845
 
ff4b330
 
 
 
 
 
 
e906845
 
ff4b330
 
 
 
 
 
 
e906845
 
ff4b330
 
 
 
 
 
 
e906845
 
ff4b330
 
 
 
 
 
 
e906845
 
ff4b330
 
 
 
 
 
 
e906845
 
ff4b330
 
 
 
 
 
 
e906845
 
ff4b330
 
 
 
 
 
 
e906845
 
ff4b330
 
 
 
 
 
 
e906845
 
ff4b330
 
 
 
 
 
 
e906845
 
ff4b330
 
 
 
 
e906845
5cbf324
 
 
e906845
 
5cbf324
 
 
e906845
 
 
5cbf324
e906845
 
 
 
5cbf324
 
 
e906845
 
5cbf324
e906845
 
 
 
5cbf324
e906845
 
 
 
5cbf324
 
 
e906845
5cbf324
 
 
 
e906845
 
 
 
 
 
5cbf324
 
 
 
 
 
 
 
e906845
 
 
 
 
 
 
5cbf324
e906845
 
 
 
 
 
 
5cbf324
 
 
 
e906845
 
 
 
 
 
 
 
5cbf324
e906845
 
 
 
 
 
5cbf324
 
e906845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cbf324
e906845
 
5cbf324
 
 
 
e906845
ff4b330
e906845
 
ff4b330
 
 
e906845
ff4b330
 
 
5cbf324
e906845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cbf324
 
 
 
 
 
ff4b330
5cbf324
e906845
 
 
 
 
 
 
 
 
 
 
 
5cbf324
 
e906845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cbf324
e906845
 
 
 
5cbf324
e906845
 
 
 
 
 
 
5cbf324
 
e906845
5cbf324
 
e906845
 
 
5cbf324
 
 
e906845
5cbf324
 
 
 
e906845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cbf324
 
 
e906845
5cbf324
 
 
 
 
 
e906845
 
 
5cbf324
 
 
e906845
5cbf324
 
 
 
e906845
 
 
 
 
 
 
 
 
 
5cbf324
e906845
5cbf324
e906845
5cbf324
e906845
 
 
 
 
5cbf324
e906845
 
5cbf324
 
e906845
 
 
 
 
5cbf324
e906845
5cbf324
 
 
 
e906845
 
 
 
 
 
 
 
 
 
 
5cbf324
e906845
 
5cbf324
 
e906845
5cbf324
e906845
 
 
5cbf324
e906845
5cbf324
e906845
5cbf324
e906845
5cbf324
e906845
5cbf324
e906845
5cbf324
 
e906845
5cbf324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff4b330
5cbf324
e906845
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import streamlit as st
import pandas as pd
import altair as alt

largest_hospitals = [
    {
        'name': 'Florida Hospital Orlando',
        'city': 'Orlando',
        'state': 'FL',
        'zip_code': '32803',
        'bed_count': 2411,
        'lat': 28.555149,
        'lng': -81.362244
    },
    {
        'name': 'Cleveland Clinic',
        'city': 'Cleveland',
        'state': 'OH',
        'zip_code': '44195',
        'bed_count': 1730,
        'lat': 41.501642,
        'lng': -81.621223
    },
    {
        'name': 'Mayo Clinic',
        'city': 'Rochester',
        'state': 'MN',
        'zip_code': '55905',
        'bed_count': 1372,
        'lat': 44.019126,
        'lng': -92.463362
    },
    {
        'name': 'NewYork-Presbyterian Hospital-Columbia and Cornell',
        'city': 'New York',
        'state': 'NY',
        'zip_code': '10032',
        'bed_count': 2332,
        'lat': 40.841708,
        'lng': -73.942635
    },
    {
        'name': 'UCHealth University of Colorado Hospital',
        'city': 'Aurora',
        'state': 'CO',
        'zip_code': '80045',
        'bed_count': 672,
        'lat': 39.743943,
        'lng': -104.834322
    },
    {
        'name': 'Houston Methodist Hospital',
        'city': 'Houston',
        'state': 'TX',
        'zip_code': '77030',
        'bed_count': 1063,
        'lat': 29.710773,
        'lng': -95.399676
    },
    {
        'name': 'Johns Hopkins Hospital',
        'city': 'Baltimore',
        'state': 'MD',
        'zip_code': '21287',
        'bed_count': 1293,
        'lat': 39.296154,
        'lng': -76.591972
    },
    {
        'name': 'Massachusetts General Hospital',
        'city': 'Boston',
        'state': 'MA',
        'zip_code': '02114',
        'bed_count': 1032,
        'lat': 42.362251,
        'lng': -71.069405
    },
    {
        'name': 'University of Michigan Hospitals-Michigan Medicine',
        'city': 'Ann Arbor',
        'state': 'MI',
        'zip_code': '48109',
        'bed_count': 1145,
        'lat': 42.285932,
        'lng': -83.730833
    },
    {
        'name': 'Mount Sinai Hospital',
        'city': 'New York',
        'state': 'NY',
        'zip_code': '10029',
        'bed_count': 1168,
        'lat': 40.788127,
        'lng': -73.952826
    }
]

largest_hospitals_df = pd.DataFrame(largest_hospitals)

def stacked_bar_chart_with_text_overlay():
    chart = alt.Chart(largest_hospitals_df).mark_bar().encode(
        y=alt.Y('state:N', sort='-x'),
        x=alt.X('bed_count:Q', stack='normalize'),
        color=alt.Color('name:N'),
        tooltip=['name', 'city', 'state', 'bed_count']
    ).properties(
        width=700,
        height=500,
        title='Largest Hospitals by State (Stacked Bar Chart with Text Overlay)'
    ).configure_axisX(
        labelAngle=-45
    )
    text = chart.mark_text(align='left', baseline='middle', dx=3).encode(
        text=alt.Text('bed_count:Q', format='.1f')
    )
    st.altair_chart(chart + text)

def bump_chart():
    chart = alt.Chart(largest_hospitals_df).transform_joinaggregate(
        rank='rank(bed_count)',
        groupby=['state']
    ).transform_filter(
        alt.datum.rank <= 3
    ).transform_window(
        y='row_number()',
        sort=[alt.SortField('bed_count', order='descending')]
    ).mark_line().encode(
        x=alt.X('bed_count:Q', title='Bed Count'),
        y=alt.Y('y:O', axis=None),
        color=alt.Color('name:N'),
        tooltip=['name', 'city', 'state', 'bed_count']
    ).properties(
        width=700,
        height=500,
        title='Largest Hospitals by State (Bump Chart)'
    )
    st.altair_chart(chart)

def radial_chart():
    chart = alt.Chart(largest_hospitals_df).mark_bar().encode(
        x=alt.X('count()', title='Count'),
        y=alt.Y('state:N', sort='-x'),
        color=alt.Color('bed_count:Q', legend=None),
        column=alt.Column('bed_count:Q', bin=alt.Bin(maxbins=10)),
        tooltip=['name', 'city', 'state', 'bed_count']
    ).properties(
        width=700,
        height=500,
        title='Largest Hospitals by State (Radial Chart)'
    )
    st.altair_chart(chart)

def trellis_area_sort_chart():
    chart = alt.Chart(largest_hospitals_df).mark_area(opacity=0.8).encode(
        x=alt.X('yearmonth(date):T', title='Date'),
        y=alt.Y('sum(revenue):Q', stack='center', axis=None),
        color=alt.Color('product:N', scale=alt.Scale(scheme='category10')),
        row=alt.Row('market:N', header=alt.Header(title='Market')),
        column=alt.Column('product:N', header=alt.Header(title='Product')),
        tooltip=[alt.Tooltip('product:N'), alt.Tooltip('revenue:Q', format='$,.0f')]
    ).properties(
        width=300,
        height=200,
        title='Trellis Area Sort Chart'
    ).configure_facet(
        spacing=0
    ).configure_view(
        stroke=None
    )
    st.altair_chart(chart)

def wind_vector_map():
    source = pd.DataFrame({
        'lat': largest_hospitals_df['lat'],
        'lon': largest_hospitals_df['lng'],
        'u': [10, 20, 30, 40, 50, -10, -20, -30, -40, -50],
        'v': [-10, -20, -30, -40, -50, 10, 20, 30, 40, 50],
        'names': largest_hospitals_df['name']
    })
    max_speed = 60

    # Create a layer of the world map
    background = alt.Chart(
        data=topo_feature('world-110m')
    ).mark_geoshape(
        fill='white',
        stroke='lightgray'
    ).properties(
        width=700,
        height=400
    ).project('naturalEarth1')

    # Add the wind vectors as arrows
    vectors = background.mark_arrow(
        length=300,
        stroke='black',
        strokeWidth=0.5
    ).encode(
        longitude='lon:Q',
        latitude='lat:Q',
        angle=alt.Angle('atan2(v, u):Q'),
        size=alt.Size(alt.Color('length:Q', legend=None), scale=alt.Scale(range=[0, 0.08]), title='Wind speed'),
        opacity=alt.Opacity(alt.Color('length:Q', legend=None), scale=alt.Scale(range=[0, 1]), title='Wind speed'),
        tooltip=['names:N', alt.Tooltip('length:Q', format='.1f')]
    ).transform_calculate(
        # Cartographic rotation for arrows
        angle=calc_wind_angle('u', 'v'),
        # Vector length
        length=calc_wind_speed('u', 'v'),
        # Limit vector length
        length=alt.datum.length > max_speed ? max_speed : alt.datum.length
    )

    st.altair_chart(background + vectors)

def table_bubble_plot():
    chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
        x=alt.X('bed_count:Q', title='Bed Count'),
        y=alt.Y('state:N', sort='-x'),
        size=alt.Size('bed_count:Q', title='Bed Count'),
        color=alt.Color('bed_count:Q', legend=None),
        tooltip=['name', 'city', 'state', 'bed_count']
    ).properties(
        width=700,
        height=500,
        title='Largest Hospitals by State (Table Bubble Plot)'
    )
    st.altair_chart(chart)

def locations_of_us_airports():
    airports = data.airports.url

    states = alt.topo_feature(data.us_10m.url, 'states')
    lookup = {'New York City': 'New York', 'Chicago': 'Illinois', 'Los Angeles': 'California', 'San Francisco': 'California', 'Houston': 'Texas'}

    chart = alt.Chart(states).mark_geoshape(
        fill='lightgray',
        stroke='white'
    ).encode(
        color=alt.Color('count()', scale=alt.Scale(scheme='yelloworangered')),
        tooltip=[alt.Tooltip('state:N'), alt.Tooltip('count():Q')]
    ).transform_lookup(
        lookup='state',
        from_=alt.LookupData(airports, 'state', ['latitude', 'longitude'])
    ).transform_fold(
        ['latitude', 'longitude'],
        as_=['key', 'value']
    ).transform_filter(
        (alt.datum.value[0] != 'NaN') & (alt.datum.value[1] != 'NaN')
    ).mark_circle(
        size=10
    ).encode(
        longitude='value:Q',
        latitude='key:Q',
        color=alt.Color('count()', scale=alt.Scale(scheme='yelloworangered')),
        tooltip=[alt.Tooltip('state:N'), alt.Tooltip('count():Q')]
    ).properties(
        width=700,
        height=500,
        title='Locations of US Airports'
    )
    st.altair_chart(chart)

def connections_among_us_airports_interactive():
    airports = data.airports.url
    routes = data.routes.url

    states = alt.topo_feature(data.us_10m.url, 'states')

    source = pd.read_json(airports)
    lookup = {'New York City': 'New York', 'Chicago': 'Illinois', 'Los Angeles': 'California', 'San Francisco': 'California', 'Houston': 'Texas'}
    source['state'] = source['state'].apply(lambda x: lookup[x] if x in lookup.keys() else x)

    base = alt.Chart(states).mark_geoshape(
        fill='lightgray',
        stroke='white'
    ).properties(
        width=700,
        height=400
    ).project('albersUsa')

    airports = base.mark_circle(size=10).encode(
        longitude='longitude:Q',
        latitude='latitude:Q',
        tooltip=['name:N', 'city:N', 'state:N', 'country:N']
    ).transform_lookup(
        lookup='iata',
        from_=alt.LookupData(airports, 'iata', ['name', 'city', 'state', 'country', 'latitude', 'longitude'])
    ).properties(title='US Airports')

    routes = base.mark_geoshape(
        stroke='black',
        strokeWidth=0.1
    ).encode(
        longitude='start_lon:Q',
        latitude='start_lat:Q',
        longitude2='end_lon:Q',
        latitude2='end_lat:Q'
    ).transform_lookup(
        lookup='start',
        from_=alt.LookupData(source, 'iata', ['state', 'latitude', 'longitude']),
        as_=['start_state', 'start_lat', 'start_lon']
    ).transform_lookup(
        lookup='end',
        from_=alt.LookupData(source, 'iata', ['state', 'latitude', 'longitude']),
        as_=['end_state', 'end_lat', 'end_lon']
    ).transform_filter(
        (alt.datum.start_lat != None) & (alt.datum.start_lon != None) & (alt.datum.end_lat != None) & (alt.datum.end_lon != None)
    ).transform_aggregate(
        count='count()',
        groupby=['start', 'start_state', 'end', 'end_state']
    ).transform_filter(
        (alt.datum['count'] > 10)
    ).transform_calculate(
        start_lon=-alt.datum.start_lon,
        end_lon=-alt.datum.end_lon
    )

    chart = (base + routes + airports).configure_view(
        width=800,
        height=500,
        stroke=None
    )
    st.altair_chart(chart)

def one_dot_per_zipcode():
    chart = alt.Chart(largest_hospitals_df).mark_circle().encode(
        longitude='lng:Q',
        latitude='lat:Q',
        size=alt.Size('bed_count:Q', title='Bed Count'),
        color=alt.Color('bed_count:Q', legend=None),
        tooltip=['name', 'city', 'state', 'zip_code', 'bed_count']
    ).properties(
        width=700,
        height=500,
        title='One Dot Per Zipcode'
    )
    st.altair_chart(chart)

def isotype_visualization_with_emoji():
    chart = alt.Chart(largest_hospitals_df).mark_point().encode(
        x=alt.X('bed_count:Q', title='Bed Count'),
        y=alt.Y('state:N', sort='-x'),
        color=alt.Color('state:N'),
        shape=alt.Shape('state:N'),
        tooltip=['name', 'city', 'state', 'zip_code', 'bed_count']
    )

    shape_lookup = {'CO': 'πŸ₯', 'FL': 'πŸ₯', 'MA': 'πŸ₯', 'MD': 'πŸ₯', 'MI': 'πŸ₯', 'MN': 'πŸ₯', 'NY': 'πŸ₯', 'OH': 'πŸ₯', 'TX': 'πŸ₯'}
    chart = chart.transform_calculate(
        shape=f'"{shape_lookup}"[datum.state]'
    )

    chart = chart.mark_text(
        align='center',
        baseline='middle',
        size=30,
        font='Segoe UI Emoji',
        dx=0,
        dy=0,
    ).encode(
        text='shape:N'
    ).properties(
        width=700,
        height=500,
        title='Isotype Visualization with Emoji'
    )
    st.altair_chart(chart)

def binned_heatmap():
    chart = alt.Chart(largest_hospitals_df).mark_rect().encode(
        x=alt.X('bed_count:Q', bin=True),
        y=alt.Y('state:N'),
        color=alt.Color('count()', scale=alt.Scale(scheme='yelloworangered')),
        tooltip=[alt.Tooltip('state:N'), alt.Tooltip('count():Q')]
    ).properties(
        width=700,
        height=500,
        title='Binned Heatmap'
    )
    st.altair_chart(chart)

def facetted_scatterplot_with_marginal_histograms():
    brush = alt.selection(type='interval', encodings=['x'])

    base = alt.Chart(largest_hospitals_df).transform_filter(
        brush
    ).properties(
        width=500,
        height=500
    )

    points = base.mark_point().encode(
        x=alt.X('bed_count:Q', title='Bed Count'),
        y=alt.Y('state:N', sort='-x', title=None),
        color=alt.Color('state:N'),
        tooltip=['name', 'city', 'state', 'zip_code', 'bed_count']
    )

    top_hist = base.mark_bar().encode(
        x=alt.X('bed_count:Q', title='Bed Count'),
        y=alt.Y('count()', title='Number of Hospitals'),
        color=alt.condition(brush, alt.ColorValue('gray'), alt.ColorValue('lightgray')),
    ).properties(
        title='Bed Count Distribution',
        width=500,
        height=100
    )

    right_hist = base.mark_bar().encode(
        y=alt.X('state:N', title='State'),
        x=alt.X('count()', title='Number of Hospitals'),
        color=alt.condition(brush, alt.ColorValue('gray'), alt.ColorValue('lightgray')),
    ).properties(
        title='Hospital Count by State',
        width=100,
        height=500
    )

    chart = ((points | top_hist) & right_hist).add_selection(
        brush
    ).configure_view(
        stroke=None
    ).properties(
        title='Facetted Scatterplot with Marginal Histograms',
        width=700,
        height=500
    )
    st.altair_chart(chart)
    
def ridgeline_plot():
    base = alt.Chart(largest_hospitals_df).transform_density(
        'bed_count',
        as_=['bed_count', 'density'],
        extent=[0, 3000],
        bandwidth=50,
        groupby=['state']
    )

    chart = base.mark_area().encode(
        x=alt.X('bed_count:Q', title='Bed Count'),
        y=alt.Y('state:N', sort='-x', title=None),
        color=alt.Color('state:N'),
        opacity=alt.Opacity('density:Q', legend=None, scale=alt.Scale(range=[0.3, 1]))
    ).properties(
        title='Ridgeline Plot',
        width=700,
        height=500
    )

    st.altair_chart(chart)


def create_sidebar():
    chart_functions = {
        'Stacked Bar Chart with Text Overlay': stacked_bar_chart,
        'Bump Chart': bump_chart,
        'Radial Chart': radial_chart,
        'Trellis Area Sort Chart': trellis_area_sort_chart,
        'Wind Vector Map': wind_vector_map,
        'Table Bubble Plot': table_bubble_plot,
        'Locations of US Airports': locations_of_us_airports,
        'Connections Among U.S. Airports Interactive': connections_among_us_airports_interactive,
        'One Dot Per Zipcode': one_dot_per_zipcode,
        'Isotype Visualization with Emoji': isotype_visualization_with_emoji,
        'Binned Heatmap': binned_heatmap,
        'Facetted Scatterplot with Marginal Histograms': facetted_scatterplot_with_marginal_histograms,
        'Ridgeline Plot': ridgeline_plot
    }
    
    st.sidebar.title('Charts')
    
    for chart_name, chart_function in chart_functions.items():
        chart_button = st.sidebar.button(f'{chart_name} {emoji(chart_name)}')
        if chart_button:
            chart_function()

def emoji(chart_name):
    emojis = {
        'Stacked Bar Chart with Text Overlay': 'πŸ“Š',
        'Bump Chart': 'πŸ“ˆ',
        'Radial Chart': '🎑',
        'Trellis Area Sort Chart': 'πŸ“‰',
        'Wind Vector Map': '🌬️',
        'Table Bubble Plot': 'πŸ’¬',
        'Locations of US Airports': '✈️',
        'Connections Among U.S. Airports Interactive': 'πŸ›«',
        'One Dot Per Zipcode': 'πŸ“',
        'Isotype Visualization with Emoji': 'πŸ˜€',
        'Binned Heatmap': 'πŸ—ΊοΈ',
        'Facetted Scatterplot with Marginal Histograms': 'πŸ”³',
        'Ridgeline Plot': 'πŸ”οΈ'
    }
    return emojis.get(chart_name, '')

create_sidebar()