using Makie, CairoMakie, GeoMakie
import Downloads
using GeoMakie.GeoJSON
using GeometryBasics
using GeoMakie.GeoInterface
using CSV,DataFrames, Tidier,Pipe
using StatsBase
country heatmap
“+proj=bacon”
1. load package
2. load geojson data
="./data/countries.geojson"
path= read(path, String)
json_str = GeoJSON.read(json_str) worldCountries
FeatureCollection with 255 Features
3. load csv
=@pipe CSV.File("./data/drinkers-had-a-heavy-session-in-past-30-days.csv")|>DataFrame|>transform(_,"Indicator:Alcohol, heavy episodic drinking (15+), drinkers only, past 30 days (%) - Sex:Both sexes"=>"drink")|>select(_,Not("Indicator:Alcohol, heavy episodic drinking (15+), drinkers only, past 30 days (%) - Sex:Both sexes"))
df
#size(df) # (189,4);
=2016
ys=@chain df begin
df@clean_names
@filter(year== !!ys)
@rename(ISO_A3=code)
#@mutate(gdp=zscore([gdp...]))
end
first(df,5)
5×4 DataFrame
Row | entity | ISO_A3 | year | drink |
---|---|---|---|---|
String | String3 | Int64 | Float64 | |
1 | Afghanistan | AFG | 2016 | 2.0 |
2 | Albania | ALB | 2016 | 44.7 |
3 | Algeria | DZA | 2016 | 19.3 |
4 | Andorra | AND | 2016 | 40.5 |
5 | Angola | AGO | 2016 | 81.6 |
4. aggregate data
= @chain DataFrame(worldCountries) @select(ISO_A3)
geo_df =@left_join(geo_df, df,"ISO_A3")
df=@pipe df.drink|>coalesce.(_, 0)
df.drink= -180:180
lons = -90:90
lats = [exp(cosd(l)) + 3(y/90) for l in lons, y in lats]
field =size(df) n,_
(255, 4)
5. plot map
= "+proj=longlat +datum=WGS84"
source = "+proj=natearth2"
dest function plot_map()
= Figure(size = (200,200), fontsize = 16)
fig
= GeoAxis(
ax 1,1];
fig[=source,
source=dest,
dest= "World GDP",
title = true,
tellheight
)
# hm1 = surface!(ax, lons, lats, field)
# translate!(hm1, 0, 0, -10)
= poly!(
hm2
ax, worldCountries;=df.drink,
color= Reverse(:plasma),
colormap = :black,
strokecolor = 0.25
strokewidth
)= Colorbar(fig[1,2]; colorrange = (1, n), colormap =(:plasma), label = "variable, color code", height = Relative(0.65))
cb
figend
plot_map()