Add a colorbar to a viz in GeoStats

Here is my code and the datafile (361 rows) - combo.csv.

Code
using GeoStats, GeoIO, CSV, DataFrames, Colors, ColorSchemes
import CairoMakie as Mke
const fname = "LAD_DEC_2023_UK_BFC.shp"

# Find min/max coordinates of metro area
function bbminmax(gt)
  bb = boundingbox(gt.geometry)
  minx, miny = coordinates(bb.min)[1:2]
  maxx, maxy = coordinates(bb.max)[1:2]
  return (minx, miny, maxx, maxy)
end

function citymap(gt, df, cr)
  fig = Mke.Figure()
  ax = []
  for (i, met) in enumerate(eachrow(df))
      longt = gt |> Filter(row -> !ismissing(row.newLocalAuthority) && row.newLocalAuthority in met.boroughs)
      push!(ax, Mke.Axis(fig[div(i - 1, 2)+1, mod(i - 1, 2)+1], title=met.metname, titlesize=10.0f0, 
                  limits=(met.xaxismin, met.xaxismax, met.yaxismin, met.yaxismax),
                  xlabel="BNG Easting",  xlabelsize=8.0f0, xtickformat="{:.0f}", xticklabelsize=6.0f0,
                  ylabel="BNG Northing", ylabelsize=8.0f0, ytickformat="{:.0f}", yticklabelsize=6.0f0))
      viz!(ax[i], longt.geometry, color=longt.col, colormap=:hawaii, colorrange=cr, segmentcolor="black", showsegments=true, segmentsize=0.3f0)
      ax[i].aspect = Mke.DataAspect()
  end
  Mke.Colorbar(fig[1:2, 3], limits=extrema(gt.rat), colormap="hawaii", scale=log10, tickformat="{:.1f}",
      ticklabelsize=8.0f0, label="Multiple of national average funding per capita", labelsize=10.0f0)
  Mke.display(fig)
end

function main()
  # Shape file from ONS: https://geoportal.statistics.gov.uk/datasets/127c4bda06314409a1fa0df505f510e6_0/explore
  print("Reading shapefile         ... ")
  @time LAs = GeoIO.load(fname) |> Rename("LAD23CD" => "LADcode")

  print("Reading combo.csv         ... ")
  @time combo = CSV.read("combo.csv", DataFrame)

  print("Processing combo data     ... ")
  @time begin
  # Funding per capita
      natave = sum(skipmissing(combo.Total_Awarded)) / sum(skipmissing(combo.subpop))
      combo.rat .= combo.Total_Awarded ./ combo.subpop ./ natave
      combo.col .= log10.(combo.rat)
      cr = extrema(combo.col)
  end

  print("Joining combo to geometry ... ")
  @time begin
      gt = georef(leftjoin!(values(LAs) |> DataFrame, combo, on=:LADcode, matchmissing=:equal), LAs.geometry) |> Filter(row -> !ismissing(row.CA))
  end

  print("Processing map areas      ... ")
  @time begin
      df = DataFrame()
      # List of boroughs in each metro area
      df.metname = ["Liverpool City Region Combined Authority Area", "Greater Manchester Combined Authority Area", "West Midlands Combined Authority Area", "Greater London"]
      df.boroughs = [["Liverpool", "Knowsley", "St. Helens", "Sefton", "Wirral", "Halton"],
          ["Bolton", "Bury", "Manchester", "Oldham", "Rochdale", "Salford", "Stockport", "Tameside", "Trafford", "Wigan"],
          ["Dudley", "Wolverhampton", "Solihull", "Walsall", "Sandwell", "Birmingham", "Coventry"],
          ["Westminster", "Ealing", "Kingston upon Thames", "Islington", "Merton", "Hackney", "Camden", "Wandsworth", "Enfield", "Southwark", "City of London", "Lambeth", "Hillingdon", "Tower Hamlets", "Kensington and Chelsea", "Greenwich", "Haringey", "Richmond upon Thames", "Hounslow", "Harrow", "Lewisham", "Hammersmith and Fulham", "Newham", "Waltham Forest", "Croydon", "Brent", "Redbridge", "Barnet", "Bromley", "Barking and Dagenham", "Havering", "Sutton", "Bexley"]]
      df.minx .= 0.0
      df.miny .= 0.0
      df.maxx .= 0.0
      df.maxy .= 0.0
      df.xrange .= 0.0
      df.yrange .= 0.0
      for met in eachrow(df)
          met.minx, met.miny, met.maxx, met.maxy = bbminmax(gt |> Filter(row -> !ismissing(row.newLocalAuthority) && row.newLocalAuthority in met.boroughs))
      end
      df.xrange = df.maxx - df.minx
      df.yrange = df.maxy - df.miny
      xaxisrange = maximum(df.xrange) * 1.05
      yaxisrange = maximum(df.yrange) * 1.05
      df.xaxismin .= (df.minx .+ df.maxx .- xaxisrange) ./ 2.0
      df.xaxismax .= (df.minx .+ df.maxx .+ xaxisrange) ./ 2.0
      df.yaxismin .= (df.miny .+ df.maxy .- yaxisrange) ./ 2.0
      df.yaxismax .= (df.miny .+ df.maxy .+ yaxisrange) ./ 2.0
  end

  print("Plotting maps             ... ")
  @time citymap(gt, df, cr)
end

main()
Data
CA	LADcode	newLocalAuthority	Total_Awarded	v4_0	subpop
England	E07000124	Ribble Valley	17126228	61,907	61907
England	E06000064	Westmorland and Furness	204034810		227640
England	E06000044	Portsmouth	181904403	206,828	206828
England	E07000076	Tendring	32501399	148,934	148934
England	E09000033	Westminster	1551248258	205,087	205087
England	E07000073	Harlow	19836025	93,374	93374
England	E07000245	West Suffolk	58102466	180,820	180820
England	E07000243	Stevenage	18555638	89,320	89320
England	E08000033	Calderdale	108848383	206,818	206818
England	E07000008	Cambridge	155324967	144,714	144714
England	E07000120	Hyndburn	27039898	82,261	82261
England	E06000065	North Yorkshire	290607762	618,847	618847
England	E08000017	Doncaster	115367820	308,705	308705
England	E06000012	North East Lincolnshire	52749726	157,197	157197
England	E07000147	North Norfolk	42407219	103,257	103257
England	E08000035	Leeds	635938290	809,036	809036
England	E07000012	South Cambridgeshire	68242390	163,002	163002
England	E06000054	Wiltshire	173373048	513,411	513411
England	E07000216	Waverley	46425610	128,878	128878
England	E07000042	Mid Devon	26862958	83,186	83186
England	E07000148	Norwich	222731508	143,118	143118
England	E07000044	South Hams	46827591	89,213	89213
England	E06000027	Torbay	55349192	139,446	139446
England	E06000059	Dorset	174142272	381,292	381292
England	E07000046	Torridge	32738961	68,475	68475
England	E06000066	Somerset	201273809	573,119	573119
England	E09000009	Ealing	110166558	366,127	366127
England	E06000052	Cornwall	429435800	572,010	572010
England	E07000009	East Cambridgeshire	27250945	88,145	88145
England	E06000025	South Gloucestershire	63117632	290,736	290736
England	E06000055	Bedford	45100774	185,761	185761
England	E08000003	Manchester	1110041952	549,853	549853
England	E07000237	Worcester	87972475	103,617	103617
England	E07000202	Ipswich	120903613	139,614	139614
England	E06000022	Bath and North East Somerset	196934258	192,423	192423
England	E07000080	Forest of Dean	25892948	87,140	87140
England	E06000057	Northumberland	219266296	321,558	321558
England	E07000082	Stroud	58646024	121,529	121529
England	E07000079	Cotswold	43188850	91,125	91125
England	E07000067	Braintree	27525481	155,710	155710
England	E07000074	Maldon	19682579	66,627	66627
England	E07000072	Epping Forest	33090331	134,909	134909
England	E06000024	North Somerset	76634329	217,399	217399
England	E06000058	Bournemouth, Christchurch and Poole	146429442	400,109	400109
England	E08000008	Tameside	69090695	231,199	231199
England	E07000047	West Devon	31814669	57,488	57488
England	E06000023	Bristol, City of	495691810	471,117	471117
England	E07000171	Bassetlaw	26489039	118,351	118351
England	E08000005	Rochdale	75868817	224,087	224087
England	E08000009	Trafford	61080849	235,546	235546
England	E08000006	Salford	210652060	270,764	270764
England	E08000012	Liverpool	503145752	484,488	484488
England	E07000123	Preston	144234169	147,617	147617
England	E08000015	Wirral	89813139	320,600	320600
England	E08000002	Bury	60232195	193,823	193823
England	E08000001	Bolton	116585737	296,041	296041
England	E06000049	Cheshire East	90296570	400,528	400528
England	E08000021	Newcastle upon Tyne	367710768	298,264	298264
England	E07000117	Burnley	51146614	94,721	94721
England	E06000063	Cumberland	187099984		306140
England	E07000125	Rossendale	26399570	71,010	71010
England	E08000014	Sefton	92179019	279,692	279692
England	E07000086	Eastleigh	150420538	136,974	136974
England	E07000115	Tonbridge and Malling	36899745	132,386	132386
England	E08000025	Birmingham	1061913344	1,142,494	1142494
England	E07000198	Staffordshire Moorlands	20721914	95,993	95993
England	E07000217	Woking	28166379	103,889	103889
England	E06000047	County Durham	332195639	521,346	521346
England	E07000226	Crawley	19078410	118,580	118580
England	E06000040	Windsor and Maidenhead	162142810	153,921	153921
England	E09000021	Kingston upon Thames	38033540	167,845	167845
England	E06000019	Herefordshire, County of	125062537	187,557	187557
England	E07000065	Wealden	36847286	160,749	160749
England	E06000037	West Berkshire	38290351	161,865	161865
England	E07000178	Oxford	175596933	160,021	160021
England	E07000222	Warwick	163721947	148,729	148729
England	E07000225	Chichester	88708250	124,531	124531
England	E06000043	Brighton and Hove	269779737	276,334	276334
England	E09000019	Islington	865003501	216,767	216767
England	E07000209	Guildford	64500555	143,929	143929
England	E07000223	Adur	14475741	64,626	64626
England	E06000036	Bracknell Forest	18878681	125,174	125174
England	E07000090	Havant	29297379	124,470	124470
England	E07000210	Mole Valley	23605664	87,608	87608
England	E07000089	Hart	5659720	100,293	100293
England	E07000227	Horsham	40415236	147,487	147487
England	E07000062	Hastings	99064240	90,959	90959
England	E06000035	Medway	93690620	279,827	279827
England	E07000238	Wychavon	42045782	133,100	133100
England	E07000239	Wyre Forest	50137890	101,786	101786
England	E08000026	Coventry	196517288	343,320	343320
England	E08000031	Wolverhampton	129180179	264,036	264036
England	E07000106	Canterbury	81338814	156,554	156554
England	E07000112	Folkestone and Hythe	49108035	110,056	110056
England	E06000045	Southampton	133827692	247,256	247256
England	E07000179	South Oxfordshire	52370186	150,024	150024
England	E08000028	Sandwell	196948660	341,729	341729
England	E06000051	Shropshire	315511696	324,716	324716
England	E07000061	Eastbourne	31132668	101,593	101593
England	E09000024	Merton	46621989	215,324	215324
England	E06000038	Reading	65768346	173,170	173170
England	E07000094	Winchester	121900371	127,916	127916
England	E08000027	Dudley	121778262	323,581	323581
England	E08000032	Bradford	307756216	546,976	546976
England	E08000030	Walsall	118240689	284,306	284306
England	E07000213	Spelthorne	7571627	102,995	102995
England	E06000042	Milton Keynes	228894376	288,201	288201
England	E07000212	Runnymede	21335815	87,739	87739
England	E07000114	Thanet	57934206	140,678	140678
England	E07000177	Cherwell	38312879	161,837	161837
England	E06000021	Stoke-on-Trent	159411724	258,037	258037
England	E07000234	Bromsgrove	19910124	99,475	99475
England	E06000015	Derby	145746503	261,136	261136
England	E07000039	South Derbyshire	18509670	108,063	108063
England	E07000032	Amber Valley	39252422	126,489	126489
England	E06000017	Rutland	12108538	41,381	41381
England	E07000037	High Peak	71747954	91,104	91104
England	E06000018	Nottingham	380656714	319,566	319566
England	E08000023	South Tyneside	59682062	147,915	147915
England	E08000024	Sunderland	169482100	274,211	274211
England	E08000037	Gateshead	223524509	196,154	196154
England	E09000012	Hackney	395602425	259,956	259956
England	E09000007	Camden	915273792	210,390	210390
England	E09000032	Wandsworth	197296888	328,367	328367
England	E09000010	Enfield	86772623	329,601	329601
England	E09000028	Southwark	573171051	306,374	306374
England	E09000001	City of London	321575633	8,618	8618
England	E09000022	Lambeth	665770100	317,498	317498
England	E09000017	Hillingdon	39514648	304,792	304792
England	E09000030	Tower Hamlets	374049415	312,273	312273
England	E09000020	Kensington and Chelsea	307431197	143,940	143940
England	E09000011	Greenwich	812345843	289,254	289254
England	E09000014	Haringey	181261693	264,130	264130
England	E09000027	Richmond upon Thames	176262413	195,232	195232
England	E09000018	Hounslow	131028168	287,940	287940
England	E09000015	Harrow	48236040	260,987	260987
England	E09000023	Lewisham	173721339	299,810	299810
England	E06000013	North Lincolnshire	40356362	169,940	169940
England	E08000019	Sheffield	540794689	554,401	554401
England	E06000014	York	124301192	201,672	201672
England	E07000041	Exeter	143187462	129,307	129307
England	E07000045	Teignbridge	50306745	135,216	135216
England	E07000078	Cheltenham	35191522	118,866	118866
England	E07000040	East Devon	24820894	152,120	152120
England	E06000010	Kingston upon Hull, City of	218763347	266,463	266463
England	E07000071	Colchester	65632564	192,424	192424
England	E07000077	Uttlesford	12662195	91,948	91948
England	E07000244	East Suffolk	114152836	246,801	246801
England	E06000030	Swindon	67306811	233,713	233713
England	E07000043	North Devon	38186118	99,435	99435
England	E07000066	Basildon	27129223	187,659	187659
England	E08000016	Barnsley	102595222	244,893	244893
England	E08000034	Kirklees	146349578	433,355	433355
England	E07000081	Gloucester	85576480	132,538	132538
England	E07000149	South Norfolk	40210364	142,527	142527
England	E06000026	Plymouth	156864733	264,727	264727
England	E08000018	Rotherham	119589023	266,183	266183
England	E08000004	Oldham	68778222	242,072	242072
England	E07000126	South Ribble	26339622	111,204	111204
England	E08000007	Stockport	73434137	295,243	295243
England	E07000221	Stratford-on-Avon	124985138	135,964	135964
England	E07000181	West Oxfordshire	30758190	115,161	115161
England	E07000220	Rugby	29103939	114,829	114829
England	E07000228	Mid Sussex	43695093	152,949	152949
England	E07000109	Gravesham	25163412	106,838	106838
England	E06000039	Slough	39294229	158,289	158289
England	E07000063	Lewes	59003684	100,194	100194
England	E07000091	New Forest	41612347	176,262	176262
England	E07000194	Lichfield	36789041	106,909	106909
England	E07000135	Oadby and Wigston	9229649	57,753	57753
England	E07000108	Dover	41180500	116,595	116595
England	E07000174	Mansfield	40326603	110,602	110602
England	E07000130	Charnwood	654719820	182,817	182817
England	E07000175	Newark and Sherwood	107852747	123,383	123383
England	E07000170	Ashfield	28910720	126,372	126372
England	E07000141	South Kesteven	33636272	143,787	143787
England	E07000139	North Kesteven	37683517	118,553	118553
England	E07000138	Lincoln	105021960	102,330	102330
England	E06000016	Leicester	284064566	366,018	366018
England	E07000036	Erewash	15670579	113,047	113047
England	E06000061	North Northamptonshire	80925293		363410
England	E07000035	Derbyshire Dales	87659938	71,681	71681
England	E07000137	East Lindsey	72398480	142,951	142951
England	E06000002	Middlesbrough	117208888	143,734	143734
England	E09000013	Hammersmith and Fulham	257659057	183,295	183295
England	E09000025	Newham	246970981	350,626	350626
England	E09000031	Waltham Forest	95473179	278,050	278050
England	E09000008	Croydon	86525712	390,506	390506
England	E09000005	Brent	304128061	338,918	338918
England	E08000036	Wakefield	141409561	353,802	353802
England	E07000102	Three Rivers	12948827	93,952	93952
England	E07000146	King's Lynn and West Norfolk	47767687	154,910	154910
England	E07000142	West Lindsey	20745398	95,570	95570
England	E06000011	East Riding of Yorkshire	95126219	343,143	343143
England	E06000004	Stockton-on-Tees	87387917	197,030	197030
England	E08000010	Wigan	94681945	329,759	329759
England	E07000127	West Lancashire	29205358	117,125	117125
England	E06000050	Cheshire West and Chester	138513040	357,699	357699
England	E06000060	Buckinghamshire	153650807	555,257	555257
England	E07000207	Elmbridge	24460662	139,369	139369
England	E08000029	Solihull	68466418	216,666	216666
England	E07000197	Stafford	79010165	137,231	137231
England	E06000062	West Northamptonshire	138510386		429010
England	E07000034	Chesterfield	42375572	103,672	103672
England	E07000131	Harborough	27883349	98,287	98287
England	E08000022	North Tyneside	96853785	209,151	209151
England	E06000005	Darlington	47388703	108,222	108222
England	E06000032	Luton	84842031	224,826	224826
England	E09000026	Redbridge	77681457	309,836	309836
England	E09000003	Barnet	82934564	388,639	388639
England	E07000011	Huntingdonshire	45220881	181,804	181804
England	E06000009	Blackpool	122212739	140,954	140954
England	E07000121	Lancaster	60144904	142,162	142162
England	E06000006	Halton	48831486	128,577	128577
England	E07000128	Wyre	17697543	112,457	112457
England	E08000011	Knowsley	63310544	154,974	154974
England	E07000180	Vale of White Horse	28066329	139,487	139487
England	E07000105	Ashford	34620052	133,178	133178
England	E09000006	Bromley	88862200	329,830	329830
England	E07000118	Chorley	36068957	117,937	117937
England	E07000240	St Albans	34372465	148,641	148641
England	E07000224	Arun	25202626	165,225	165225
England	E07000084	Basingstoke and Deane	27816193	185,656	185656
England	E07000064	Rother	38127664	93,429	93429
England	E07000107	Dartford	15342438	116,777	116777
England	E07000211	Reigate and Banstead	18317948	151,423	151423
England	E06000020	Telford and Wrekin	52760459	185,842	185842
England	E06000046	Isle of Wight	68196344	140,889	140889
England	E07000176	Rushcliffe	154844830	119,438	119438
England	E09000002	Barking and Dagenham	63604751	218,534	218534
England	E09000016	Havering	46974062	262,022	262022
England	E07000144	Broadland	17573628	132,247	132247
England	E06000056	Central Bedfordshire	58841118	295,541	295541
England	E06000031	Peterborough	81375286	216,349	216349
England	E06000033	Southend-on-Sea	86467480	180,601	180601
England	E06000008	Blackburn with Darwen	61770088	154,922	154922
England	E08000013	St. Helens	68590883	183,391	183391
England	E07000113	Swale	40789324	152,223	152223
England	E07000199	Tamworth	13032508	78,838	78838
England	E07000110	Maidstone	69725622	176,712	176712
England	E06000007	Warrington	67265990	211,227	211227
England	E07000136	Boston	26544103	70,798	70798
England	E07000038	North East Derbyshire	18979618	102,315	102315
England	E07000173	Gedling	17814152	117,298	117298
England	E06000001	Hartlepool	48563233	92,571	92571
England	E07000200	Babergh	26849588	92,723	92723
England	E07000099	North Hertfordshire	23835846	133,571	133571
England	E07000242	East Hertfordshire	40955056	150,687	150687
England	E07000083	Tewkesbury	11563022	95,429	95429
England	E07000195	Newcastle-under-Lyme	35324678	123,025	123025
England	E07000214	Surrey Heath	10590823	90,645	90645
England	E07000196	South Staffordshire	17455415	110,793	110793
England	E07000219	Nuneaton and Bedworth	23227994	134,291	134291
England	E07000172	Broxtowe	18985823	110,813	110813
England	E06000003	Redcar and Cleveland	64845306	136,616	136616
England	E07000068	Brentwood	9275545	77,103	77103
England	E07000070	Chelmsford	91187707	181,763	181763
England	E06000034	Thurrock	31957677	175,902	175902
England	E07000122	Pendle	26743993	95,789	95789
England	E07000235	Malvern Hills	40066998	79,973	79973
England	E07000236	Redditch	14798632	86,996	86996
England	E07000111	Sevenoaks	29843133	120,841	120841
England	E09000029	Sutton	42463929	209,517	209517
England	E07000203	Mid Suffolk	26581744	103,417	103417
England	E07000132	Hinckley and Bosworth	12495354	113,660	113660
England	E07000134	North West Leicestershire	31891278	105,011	105011
England	E07000241	Welwyn Hatfield	33803702	119,538	119538
England	E07000092	Rushmoor	11266353	100,068	100068
England	E06000041	Wokingham	34567518	178,169	178169
England	E07000229	Worthing	24133376	111,657	111657
England	E07000103	Watford	39000926	102,451	102451
England	E07000119	Fylde	19160818	81,845	81845
England	E07000093	Test Valley	17612692	131,190	131190
England	E07000116	Tunbridge Wells	37632244	115,681	115681
England	E07000085	East Hampshire	25436786	126,199	126199
England	E07000088	Gosport	18022521	82,178	82178
England	E07000215	Tandridge	14540853	88,143	88143
England	E07000033	Bolsover	38745572	80,476	80476
England	E07000133	Melton	16301728	51,773	51773
England	E07000143	Breckland	37934757	142,164	142164
England	E07000129	Blaby	21313880	103,271	103271
England	E07000087	Fareham	17026915	114,993	114993
England	E09000004	Bexley	41183878	246,543	246543
England	E07000096	Dacorum	37047835	155,217	155217
England	E07000069	Castle Point	9411296	89,708	89708
England	E07000193	East Staffordshire	21925018	124,477	124477
England	E07000192	Cannock Chase	18559989	100,590	100590
England	E07000145	Great Yarmouth	49749393	100,146	100146
England	E07000010	Fenland	15801999	102,742	102742
England	E07000208	Epsom and Ewell	15222092	80,998	80998
England	E07000140	South Holland	13974115	95,485	95485
England	E07000075	Rochford	4676934	86,186	86186
England	E07000095	Broxbourne	16836646	99,138	99138
England	E07000098	Hertsmere	20700299	108,105	108105
England	E07000218	North Warwickshire	18624934	65,340	65340
England	E06000053	Isles of Scilly	3458612	2,271	2271
Status `C:\Users\TGebbels\...\Documents\DCMS Database\Project.toml`
  [6e4b80f9] BenchmarkTools v1.5.0
  [336ed68f] CSV v0.10.13
  [13f3f980] CairoMakie v0.11.10
  [35d6a980] ColorSchemes v3.24.0
  [5ae59095] Colors v0.12.10
  [a93c6f00] DataFrames v1.6.1
  [8bb1440f] DelimitedFiles v1.9.1
  [f5a160d5] GeoIO v1.12.12
  [dcc97b0b] GeoStats v0.54.0
  [ee78f7c6] Makie v0.20.9
  [16e07271] PlutoStyles v0.1.11
  [08abe8d2] PrettyTables v2.3.1
  [69024149] StringEncodings v0.3.7
  [f269a46b] TimeZones v1.14.0
  [ade2ca70] Dates
  [de0858da] Printf
1 Like

As a side note, you should be able to specify the aspect ratio directly in the Makie.Axis with:

Mke.Axis(..., aspect = Mke.DataAspect())
1 Like

@TimG please update the environment to get Colorfy.jl v0.1.4 with the fix. It should be out in a few minutes.

1 Like

Yes, this now works great! Thanks!

1 Like

@TimG we added a section to the viz chapter in the GDSJL book. Thanks for raising the issue and for suggesting improvements to the color option.

2 Likes