Skip to content

Instantly share code, notes, and snippets.

@amitkaps
Created August 12, 2017 11:29
Show Gist options
  • Save amitkaps/67bd6dcb2af300a2b76f1e2351c1afdc to your computer and use it in GitHub Desktop.
Save amitkaps/67bd6dcb2af300a2b76f1e2351c1afdc to your computer and use it in GitHub Desktop.
Unit Chart Small Multiple

This is a Unit Chart with Small Multiples. Requires pre-processing the dataset to the create id and sub-id field to use for positioning. Uses the Vega-lite library to do so in a declarative manner.

id subid country continent year lifeExp pop gdpPercap
1 1 Algeria Africa 2007 72.301 33333216 6223.367465
2 2 Angola Africa 2007 42.731 12420476 4797.231267
3 3 Benin Africa 2007 56.728 8078314 1441.284873
4 4 Botswana Africa 2007 50.728 1639131 12569.85177
5 5 Burkina Faso Africa 2007 52.295 14326203 1217.032994
6 6 Burundi Africa 2007 49.58 8390505 430.0706916
7 7 Cameroon Africa 2007 50.43 17696293 2042.09524
8 8 Central African Republic Africa 2007 44.741 4369038 706.016537
9 9 Chad Africa 2007 50.651 10238807 1704.063724
10 10 Comoros Africa 2007 65.152 710960 986.1478792
11 11 Congo Dem. Rep. Africa 2007 46.462 64606759 277.5518587
12 12 Congo Rep. Africa 2007 55.322 3800610 3632.557798
13 13 Cote d'Ivoire Africa 2007 48.328 18013409 1544.750112
14 14 Djibouti Africa 2007 54.791 496374 2082.481567
15 15 Egypt Africa 2007 71.338 80264543 5581.180998
16 16 Equatorial Guinea Africa 2007 51.579 551201 12154.08975
17 17 Eritrea Africa 2007 58.04 4906585 641.3695236
18 18 Ethiopia Africa 2007 52.947 76511887 690.8055759
19 19 Gabon Africa 2007 56.735 1454867 13206.48452
20 20 Gambia Africa 2007 59.448 1688359 752.7497265
21 21 Ghana Africa 2007 60.022 22873338 1327.60891
22 22 Guinea Africa 2007 56.007 9947814 942.6542111
23 23 Guinea-Bissau Africa 2007 46.388 1472041 579.231743
24 24 Kenya Africa 2007 54.11 35610177 1463.249282
25 25 Lesotho Africa 2007 42.592 2012649 1569.331442
26 26 Liberia Africa 2007 45.678 3193942 414.5073415
27 27 Libya Africa 2007 73.952 6036914 12057.49928
28 28 Madagascar Africa 2007 59.443 19167654 1044.770126
29 29 Malawi Africa 2007 48.303 13327079 759.3499101
30 30 Mali Africa 2007 54.467 12031795 1042.581557
31 31 Mauritania Africa 2007 64.164 3270065 1803.151496
32 32 Mauritius Africa 2007 72.801 1250882 10956.99112
33 33 Morocco Africa 2007 71.164 33757175 3820.17523
34 34 Mozambique Africa 2007 42.082 19951656 823.6856205
35 35 Namibia Africa 2007 52.906 2055080 4811.060429
36 36 Niger Africa 2007 56.867 12894865 619.6768924
37 37 Nigeria Africa 2007 46.859 135031164 2013.977305
38 38 Reunion Africa 2007 76.442 798094 7670.122558
39 39 Rwanda Africa 2007 46.242 8860588 863.0884639
40 40 Sao Tome and Principe Africa 2007 65.528 199579 1598.435089
41 41 Senegal Africa 2007 63.062 12267493 1712.472136
42 42 Sierra Leone Africa 2007 42.568 6144562 862.5407561
43 43 Somalia Africa 2007 48.159 9118773 926.1410683
44 44 South Africa Africa 2007 49.339 43997828 9269.657808
45 45 Sudan Africa 2007 58.556 42292929 2602.394995
46 46 Swaziland Africa 2007 39.613 1133066 4513.480643
47 47 Tanzania Africa 2007 52.517 38139640 1107.482182
48 48 Togo Africa 2007 58.42 5701579 882.9699438
49 49 Tunisia Africa 2007 73.923 10276158 7092.923025
50 50 Uganda Africa 2007 51.542 29170398 1056.380121
51 51 Zambia Africa 2007 42.384 11746035 1271.211593
52 52 Zimbabwe Africa 2007 43.487 12311143 469.7092981
53 1 Argentina Americas 2007 75.32 40301927 12779.37964
54 2 Bolivia Americas 2007 65.554 9119152 3822.137084
55 3 Brazil Americas 2007 72.39 190010647 9065.800825
56 4 Canada Americas 2007 80.653 33390141 36319.23501
57 5 Chile Americas 2007 78.553 16284741 13171.63885
58 6 Colombia Americas 2007 72.889 44227550 7006.580419
59 7 Costa Rica Americas 2007 78.782 4133884 9645.06142
60 8 Cuba Americas 2007 78.273 11416987 8948.102923
61 9 Dominican Republic Americas 2007 72.235 9319622 6025.374752
62 10 Ecuador Americas 2007 74.994 13755680 6873.262326
63 11 El Salvador Americas 2007 71.878 6939688 5728.353514
64 12 Guatemala Americas 2007 70.259 12572928 5186.050003
65 13 Haiti Americas 2007 60.916 8502814 1201.637154
66 14 Honduras Americas 2007 70.198 7483763 3548.330846
67 15 Jamaica Americas 2007 72.567 2780132 7320.880262
68 16 Mexico Americas 2007 76.195 108700891 11977.57496
69 17 Nicaragua Americas 2007 72.899 5675356 2749.320965
70 18 Panama Americas 2007 75.537 3242173 9809.185636
71 19 Paraguay Americas 2007 71.752 6667147 4172.838464
72 20 Peru Americas 2007 71.421 28674757 7408.905561
73 21 Puerto Rico Americas 2007 78.746 3942491 19328.70901
74 22 Trinidad and Tobago Americas 2007 69.819 1056608 18008.50924
75 23 United States Americas 2007 78.242 301139947 42951.65309
76 24 Uruguay Americas 2007 76.384 3447496 10611.46299
77 25 Venezuela Americas 2007 73.747 26084662 11415.80569
78 1 Afghanistan Asia 2007 43.828 31889923 974.5803384
79 2 Bahrain Asia 2007 75.635 708573 29796.04834
80 3 Bangladesh Asia 2007 64.062 150448339 1391.253792
81 4 Cambodia Asia 2007 59.723 14131858 1713.778686
82 5 China Asia 2007 72.961 1318683096 4959.114854
83 6 Hong Kong China Asia 2007 82.208 6980412 39724.97867
84 7 India Asia 2007 64.698 1110396331 2452.210407
85 8 Indonesia Asia 2007 70.65 223547000 3540.651564
86 9 Iran Asia 2007 70.964 69453570 11605.71449
87 10 Iraq Asia 2007 59.545 27499638 4471.061906
88 11 Israel Asia 2007 80.745 6426679 25523.2771
89 12 Japan Asia 2007 82.603 127467972 31656.06806
90 13 Jordan Asia 2007 72.535 6053193 4519.461171
91 14 Korea Dem. Rep. Asia 2007 67.297 23301725 1593.06548
92 15 Korea Rep. Asia 2007 78.623 49044790 23348.13973
93 16 Kuwait Asia 2007 77.588 2505559 47306.98978
94 17 Lebanon Asia 2007 71.993 3921278 10461.05868
95 18 Malaysia Asia 2007 74.241 24821286 12451.6558
96 19 Mongolia Asia 2007 66.803 2874127 3095.772271
97 20 Myanmar Asia 2007 62.069 47761980 944
98 21 Nepal Asia 2007 63.785 28901790 1091.359778
99 22 Oman Asia 2007 75.64 3204897 22316.19287
100 23 Pakistan Asia 2007 65.483 169270617 2605.94758
101 24 Philippines Asia 2007 71.688 91077287 3190.481016
102 25 Saudi Arabia Asia 2007 72.777 27601038 21654.83194
103 26 Singapore Asia 2007 79.972 4553009 47143.17964
104 27 Sri Lanka Asia 2007 72.396 20378239 3970.095407
105 28 Syria Asia 2007 74.143 19314747 4184.548089
106 29 Taiwan Asia 2007 78.4 23174294 28718.27684
107 30 Thailand Asia 2007 70.616 65068149 7458.396327
108 31 Vietnam Asia 2007 74.249 85262356 2441.576404
109 32 West Bank and Gaza Asia 2007 73.422 4018332 3025.349798
110 33 Yemen Rep. Asia 2007 62.698 22211743 2280.769906
111 1 Albania Europe 2007 76.423 3600523 5937.029526
112 2 Austria Europe 2007 79.829 8199783 36126.4927
113 3 Belgium Europe 2007 79.441 10392226 33692.60508
114 4 Bosnia and Herzegovina Europe 2007 74.852 4552198 7446.298803
115 5 Bulgaria Europe 2007 73.005 7322858 10680.79282
116 6 Croatia Europe 2007 75.748 4493312 14619.22272
117 7 Czech Republic Europe 2007 76.486 10228744 22833.30851
118 8 Denmark Europe 2007 78.332 5468120 35278.41874
119 9 Finland Europe 2007 79.313 5238460 33207.0844
120 10 France Europe 2007 80.657 61083916 30470.0167
121 11 Germany Europe 2007 79.406 82400996 32170.37442
122 12 Greece Europe 2007 79.483 10706290 27538.41188
123 13 Hungary Europe 2007 73.338 9956108 18008.94444
124 14 Iceland Europe 2007 81.757 301931 36180.78919
125 15 Ireland Europe 2007 78.885 4109086 40675.99635
126 16 Italy Europe 2007 80.546 58147733 28569.7197
127 17 Montenegro Europe 2007 74.543 684736 9253.896111
128 18 Netherlands Europe 2007 79.762 16570613 36797.93332
129 19 Norway Europe 2007 80.196 4627926 49357.19017
130 20 Poland Europe 2007 75.563 38518241 15389.92468
131 21 Portugal Europe 2007 78.098 10642836 20509.64777
132 22 Romania Europe 2007 72.476 22276056 10808.47561
133 23 Serbia Europe 2007 74.002 10150265 9786.534714
134 24 Slovak Republic Europe 2007 74.663 5447502 18678.31435
135 25 Slovenia Europe 2007 77.926 2009245 25768.25759
136 26 Spain Europe 2007 80.941 40448191 28821.0637
137 27 Sweden Europe 2007 80.884 9031088 33859.74835
138 28 Switzerland Europe 2007 81.701 7554661 37506.41907
139 29 Turkey Europe 2007 71.777 71158647 8458.276384
140 30 United Kingdom Europe 2007 79.425 60776238 33203.26128
141 1 Australia Oceania 2007 81.235 20434176 34435.36744
142 2 New Zealand Oceania 2007 80.204 4115771 25185.00911
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega/3.0.0/vega.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-lite/2.0.0-beta.13/vega-lite.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-embed/3.0.0-beta.19/vega-embed.js"></script>
</head>
<body>
<div id="vis"></div>
<script>
const spec = "unit-chart-multiple.vl.json"
var opts = {"actions": {export: false, source: false, editor: false}};
vega.embed('#vis', spec, opts);
</script>
</body>
The MIT License (MIT)
Copyright (c) 2017 Amit Kapoor
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
{
"$schema": "https://vega.github.io/schema/vega-lite/v2.json",
"description": "A Unit Chart with Small Multiples",
"data": {"url": "gapminder.csv"},
"mark": "rect",
"transform": [
{"calculate": "ceil(datum.subid/ 7)", "as": "Y"},
{"calculate": "datum.subid - (datum.Y - 1) * 7", "as": "X"}
],
"encoding": {
"x": {
"field": "X",
"type": "ordinal",
"axis": null
},
"y":{
"field": "Y",
"type": "ordinal",
"axis": null
},
"color":{
"field": "continent",
"type": "nominal"
},
"column": {
"field": "continent",
"type": "nominal"
},
"tooltip":{
"field": "country",
"type": "nominal"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment