using Makie, CairoMakie, GeoMakie
import Downloads
using GeoMakie.GeoJSON
using GeometryBasics
using GeoMakie.GeoInterface
using CSV,DataFrames, Tidier,Pipe
using StatsBase
country heatmap
1. load package
2. load geojson data
="./plots-gallery-data/countries.geojson"
path= read(path, String)
json_str = GeoJSON.read(json_str) worldCountries
FeatureCollection with 255 Features
3. load csv
=@pipe CSV.File("./plots-gallery-data/gdp-per-capita-penn-world-table.csv")|>DataFrame|>transform(_,"GDP per capita (output, multiple price benchmarks)"=>"gdp")|>select(_,Not("GDP per capita (output, multiple price benchmarks)"))
df=2019
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 | gdp |
---|---|---|---|---|
String | String3 | Int64 | Float64 | |
1 | Albania | ALB | 2019 | 12531.8 |
2 | Algeria | DZA | 2019 | 11787.5 |
3 | Angola | AGO | 2019 | 7159.58 |
4 | Anguilla | AIA | 2019 | 15177.9 |
5 | Argentina | ARG | 2019 | 21826.8 |
4. aggregate data
= @chain DataFrame(worldCountries) @select(ISO_A3)
geo_df =@left_join(geo_df, df,"ISO_A3")
df=@pipe df.gdp|>coalesce.(_, 0)
df.gdp= -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
function plot_map()
= Figure(size = (200,200), fontsize = 16)
fig
= GeoAxis(
ax 1,1];
fig[="+proj=wintri",
dest= "World GDP",
title = true,
tellheight
)
= surface!(ax, lons, lats, field)
hm1 translate!(hm1, 0, 0, -10)
= poly!(
hm2
ax, worldCountries;=df.gdp,
color= Reverse(:plasma),
colormap = :black,
strokecolor = 0.25
strokewidth
)= Colorbar(fig[1,2]; colorrange = (1, n), colormap = Reverse(:plasma), label = "variable, color code", height = Relative(0.65))
cb
figend
plot_map()
┌ Warning: Could not find font regular, using TeX Gyre Heros Makie
└ @ Makie ~/.julia/packages/Makie/RgxaV/src/conversions.jl:1119