text: '''
  Isotype Visualization
  =====================
  Isotype Visualization shows the distribution of animals across UK and US.
  Inspired by [Only An Ocean Between, 1943](http://www.thomwhite.co.uk/?p=1303). Population Live Stock, p.13.
  This is adapted from Vega-Lite example https://vega.github.io/editor/#/examples/vega-lite/isotype_bar_chart
  '''
  # category: case studies
  import pandas as pd
  import altair as alt
  
  df =  pd.DataFrame([
        {'country': 'Great Britain', 'animal': 'cattle', 'col': 3},
        {'country': 'Great Britain', 'animal': 'cattle', 'col': 2},
        {'country': 'Great Britain', 'animal': 'cattle', 'col': 1},
        {'country': 'Great Britain', 'animal': 'pigs', 'col': 2},
        {'country': 'Great Britain', 'animal': 'pigs', 'col': 1},
        {'country': 'Great Britain', 'animal': 'sheep', 'col': 10},
        {'country': 'Great Britain', 'animal': 'sheep', 'col': 9},
        {'country': 'Great Britain', 'animal': 'sheep', 'col': 8},
        {'country': 'Great Britain', 'animal': 'sheep', 'col': 7},
        {'country': 'Great Britain', 'animal': 'sheep', 'col': 6},
        {'country': 'Great Britain', 'animal': 'sheep', 'col': 5},
        {'country': 'Great Britain', 'animal': 'sheep', 'col': 4},
        {'country': 'Great Britain', 'animal': 'sheep', 'col': 3},
        {'country': 'Great Britain', 'animal': 'sheep', 'col': 2},
        {'country': 'Great Britain', 'animal': 'sheep', 'col': 1},
        {'country': 'United States', 'animal': 'cattle', 'col': 9},
        {'country': 'United States', 'animal': 'cattle', 'col': 8},
        {'country': 'United States', 'animal': 'cattle', 'col': 7},
        {'country': 'United States', 'animal': 'cattle', 'col': 6},
        {'country': 'United States', 'animal': 'cattle', 'col': 5},
        {'country': 'United States', 'animal': 'cattle', 'col': 4},
        {'country': 'United States', 'animal': 'cattle', 'col': 3},
        {'country': 'United States', 'animal': 'cattle', 'col': 2},
        {'country': 'United States', 'animal': 'cattle', 'col': 1},
        {'country': 'United States', 'animal': 'pigs', 'col': 6},
        {'country': 'United States', 'animal': 'pigs', 'col': 5},
        {'country': 'United States', 'animal': 'pigs', 'col': 4},
        {'country': 'United States', 'animal': 'pigs', 'col': 3},
        {'country': 'United States', 'animal': 'pigs', 'col': 2},
        {'country': 'United States', 'animal': 'pigs', 'col': 1},
        {'country': 'United States', 'animal': 'sheep', 'col': 7},
        {'country': 'United States', 'animal': 'sheep', 'col': 6},
        {'country': 'United States', 'animal': 'sheep', 'col': 5},
        {'country': 'United States', 'animal': 'sheep', 'col': 4},
        {'country': 'United States', 'animal': 'sheep', 'col': 3},
        {'country': 'United States', 'animal': 'sheep', 'col': 2},
        {'country': 'United States', 'animal': 'sheep', 'col': 1}
      ])
  
  domains = ['person', 'cattle', 'pigs', 'sheep']
  
  shape_scale = alt.Scale(
      domain=domains,
      range=[
          'M1.7 -1.7h-0.8c0.3 -0.2 0.6 -0.5 0.6 -0.9c0 -0.6 -0.4 -1 -1 -1c-0.6 0 -1 0.4 -1 1c0 0.4 0.2 0.7 0.6 0.9h-0.8c-0.4 0 -0.7 0.3 -0.7 0.6v1.9c0 0.3 0.3 0.6 0.6 0.6h0.2c0 0 0 0.1 0 0.1v1.9c0 0.3 0.2 0.6 0.3 0.6h1.3c0.2 0 0.3 -0.3 0.3 -0.6v-1.8c0 0 0 -0.1 0 -0.1h0.2c0.3 0 0.6 -0.3 0.6 -0.6v-2c0.2 -0.3 -0.1 -0.6 -0.4 -0.6z',
          'M4 -2c0 0 0.9 -0.7 1.1 -0.8c0.1 -0.1 -0.1 0.5 -0.3 0.7c-0.2 0.2 1.1 1.1 1.1 1.2c0 0.2 -0.2 0.8 -0.4 0.7c-0.1 0 -0.8 -0.3 -1.3 -0.2c-0.5 0.1 -1.3 1.6 -1.5 2c-0.3 0.4 -0.6 0.4 -0.6 0.4c0 0.1 0.3 1.7 0.4 1.8c0.1 0.1 -0.4 0.1 -0.5 0c0 0 -0.6 -1.9 -0.6 -1.9c-0.1 0 -0.3 -0.1 -0.3 -0.1c0 0.1 -0.5 1.4 -0.4 1.6c0.1 0.2 0.1 0.3 0.1 0.3c0 0 -0.4 0 -0.4 0c0 0 -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 0.1 -0.2 -0.1 -0.4 -0.1c-0.2 -0.1 -0.3 0.1 -0.4 0c-0.2 -0.1 -0.3 0.1 -0.5 0c-0.2 -0.1 -0.1 0 -0.3 0.3c-0.2 0.3 -0.4 0.3 -0.4 0.3l0.2 1.1l-0.3 0l-0.2 -1.1c0 0 -0.4 -0.6 -0.5 -0.4c-0.1 0.3 -0.1 0.4 -0.3 0.4c-0.1 -0.1 -0.2 1.1 -0.2 1.1l-0.3 0l0.2 -1.1c0 0 -0.3 -0.1 -0.3 -0.5c0 -0.3 0.1 -0.5 0.1 -0.7c0.1 -0.2 -0.1 -1 -0.2 -1.1c-0.1 -0.2 -0.2 -0.8 -0.2 -0.8c0 0 -0.1 -0.5 0.4 -0.8z'
      ]
  )
  
  color_scale = alt.Scale(
      domain=domains,
      range=['rgb(162,160,152)', 'rgb(194,81,64)', 'rgb(93,93,93)', 'rgb(91,131,149)']
  )
  
  alt.Chart(df).mark_point(filled=True).encode(
      alt.X('col:O', axis=None),
      alt.Y('animal:O', axis=None),
      alt.Row('country:N', header=alt.Header(title='')),
      alt.Shape('animal:N', legend=None, scale=shape_scale),
      alt.Color('animal:N', legend=None, scale=color_scale),
      alt.OpacityValue(1),
      alt.SizeValue(200)
  ).properties(width=800, height=200)
  