Skip to content

Commit b179ff6

Browse files
committed
up makie
1 parent dcf9c5f commit b179ff6

File tree

8 files changed

+3596
-290
lines changed

8 files changed

+3596
-290
lines changed

notebooks/makie/Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[deps]
2+
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
3+
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
4+
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
5+
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"

notebooks/makie/data.jl

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
using CairoMakie
2+
using DataFrames
3+
using JLD2
4+
5+
# notice you need to load the DataFrames package first before trying to look at the data.
6+
# why?
7+
"function to load data from disk"
8+
load_data() = jldopen("data.jld2")["d"]
9+
10+
11+
12+
13+
14+
15+
function figure1_plot(d::DataFrame,offs::OrderedDict)
16+
17+
K = length(unique(d.region))
18+
19+
d[!,:LIBGEO] .= ""
20+
ko = collect(keys(offs))
21+
for i in 1:length(ko)
22+
d[d.region .== i, :LIBGEO] .= ko[i]
23+
end
24+
# compute shares of rural and urban land rent over income
25+
d.rural_rent = 100 .* d.ρr .* (d.Sr .+ d.Srh) ./ d.GDP
26+
d.urban_rent = 100 .* d.iq ./ d.GDP
27+
28+
# create the aggregate/average city
29+
agg_city = combine(
30+
groupby(d, :year),
31+
:Lu => mean => :Lu,
32+
:Lr => mean => :Lr,
33+
:ρr => mean => :ρr,
34+
:pr => mean => :pr,
35+
:cityarea => mean => :cityarea,
36+
[:Cr,:Cu,:Ch] => ((x,y,z) -> (rshare = sum(x) / sum((x + y + z)),
37+
ushare = sum(y) / sum((x + y + z)),
38+
hshare = sum(z) / sum((x + y + z)))) => AsTable,
39+
[:citydensity,:Lu] => ((x,y) -> mean(x)) => :density,
40+
[:avgd_n,:Lu] => ((x,y) -> mean(x)) => :density_n,
41+
[:dr_n,:Lu] => ((x,y) -> mean(x)) => :dr_n,
42+
[:d0_n,:Lu] => ((x,y) -> mean(x)) => :d0_n,
43+
[:ρr,:Sr,:Srh,:GDP] => ((x,y,z,g) -> 100 * sum(x .* (y .+ z)) / sum(g)) => :rural_rent,
44+
[:iq,:GDP] => ((r,p) -> 100 * sum(r) / sum(p)) => :urban_rent,
45+
)
46+
47+
normalizers = @chain agg_city begin
48+
subset(:year => ByRow(==(1840)))
49+
select(:Lu,:cityarea,:density, :ρr)
50+
end
51+
52+
# plotter setup
53+
idx = subset(d, :it => ieq(1))
54+
labs = reshape(idx.LIBGEO, 1,K)
55+
cols = reshape([:darkgreen,:darkgreen,:firebrick,:firebrick], 1,K)
56+
styles = reshape([:solid,:dot,:solid,:dot], 1,K)
57+
widths = reshape([3,3,3,3], 1,K)
58+
59+
def_theme()
60+
61+
pl = Dict()
62+
63+
# row 1
64+
# Lr
65+
pl[:Lr] = plot(agg_city.year, agg_city.Lr, color = :darkgreen, legend = false, size = panelsizef(npanels = 3))
66+
67+
# spending
68+
pl[:spending] = @df agg_city plot(:year, [:rshare, :ushare, :hshare], color = [:darkgreen :firebrick :darkblue], linestyle = [:solid :dot :dashdot], legend = :topleft, label = ["Rural Good" "Urban Good" "Housing"],ylims = (0,0.9), size = panelsizef(npanels = 3))
69+
70+
# food price - only aggregate
71+
pl[:pr] = @df agg_city plot(:year, :pr, color = :darkgreen, leg = false, size = panelsizef(npanels = 3))
72+
73+
# row 2
74+
# Urban area and population
75+
pl[:LuArea] = @df agg_city plot(:year, [:Lu ./ normalizers.Lu,
76+
:cityarea ./ normalizers.cityarea], yscale = :log10,yticks = [1,2,10,50,100], yformatter = x -> string(round(Int,x)),
77+
color = [reds()[1] golds()[1]],
78+
linestyle = [:solid :dash],
79+
label = ["Urban population" "Urban area"], size = panelsizef(npanels = 3))
80+
81+
# Urban densities
82+
pl[:aggDensities] = @df agg_city plot(:year, [:density_n ,
83+
:d0_n ,
84+
:dr_n ],
85+
color = [reds()[1] golds()[1] blues()[3]],
86+
linestyle = [:solid :dash :dashdot], size = panelsizef(npanels = 3),
87+
label = ["Average" "Central" "Fringe"])
88+
pl[:aggDensities_log] = @df agg_city plot(:year, [:density_n ,
89+
:d0_n ,
90+
:dr_n ],
91+
color = [reds()[1] golds()[1] blues()[3]],
92+
linestyle = [:solid :dash :dashdot], size = panelsizef(npanels = 3),
93+
label = ["Average" "Central" "Fringe"], yscale = :log10, yticks = [0.05,0.1,0.25,1], yformatter = x -> string(round(x,digits=2)))
94+
# pl[:aggDensities] = @df agg_city plot(:year, [:density ,
95+
# :d0 ,
96+
# :dr],
97+
# color = [reds()[1] golds()[1] blues()[3]],
98+
# linestyle = [:solid :dash :dashdot])
99+
100+
# rural rent and urban rent
101+
pl[:landrents] = @df agg_city plot(:year, [:rural_rent :urban_rent], size = panelsizef(npanels = 3), labels = ["Rural Rents" "Urban Rents"], color = [greens()[3] reds()[1]], yticks = 0:2:18, linestyle = [:solid :dot])
102+
# plot!(pl[:ruralrents],agg_city.year, agg_city.rural_rent, color = :grey, lw = 3, label = "")
103+
104+
# third row
105+
# spreads
106+
pl[:Lu_spread] = @df d plot(:year, :Lu ./ normalizers.Lu, group = :region, color = cols, label = labs, linestyle = styles, size = panelsizef(npanels = 3))
107+
108+
pl[:cityarea_spread] = @df d plot(:year, :cityarea ./ normalizers.cityarea, group = :region, color = cols, label = labs, linestyle = styles, size = panelsizef(npanels = 3), yscale = :log10,yticks = [1,2,5,30,100], yformatter = x -> string(round(x, digits = 2)))
109+
110+
# normalize by first obs of aggregated city.
111+
# and do a second panel with fringe and center
112+
# average density
113+
pl[:density_spread_log] = @df d plot(:year, :citydensity ./ normalizers.density, group = :region, color = cols, label = labs, linestyle = styles, size = panelsizef(npanels = 3), yscale = :log10, yticks = [0.01,0.05,0.2,0.5,1], yformatter = x -> string(round(x,digits=2)))
114+
115+
pl[:density_spread] = @df d plot(:year, :citydensity ./ normalizers.density, group = :region, color = cols, label = labs, linestyle = styles, size = panelsizef(npanels = 3))
116+
# # add aggregate
117+
# plot!(pl[:avg_density], agg_city.year, agg_city.density ./ normalizers.density, color = :grey, lw = 3, label = "")
118+
119+
# # fringe density
120+
# pl[:fringe_density] = @df d plot(:year, :dr ./ normalizers.dr, group = :region, color = cols, label = labs, linestyle = styles, linewidth = widths, size = panelsizef(npanels = 3), yscale = :log10, yticks = [0.01,0.05,0.2,0.5,1], yformatter = x -> string(round(x,digits=2)))
121+
# # add aggregate
122+
# plot!(pl[:fringe_density], agg_city.year, agg_city.dr ./ normalizers.dr, color = :grey, lw = 3, label = "")
123+
124+
# # central density
125+
# pl[:central_density] = @df d plot(:year, :d0 ./ normalizers.d0, group = :region, color = cols, label = labs, linestyle = styles, linewidth = widths, size = panelsizef(npanels = 3), yscale = :log10, yticks = [0.01,0.05,0.2,0.5,1], yformatter = x -> string(round(x,digits=2)))
126+
# # add aggregate
127+
# plot!(pl[:central_density], agg_city.year, agg_city.d0 ./ normalizers.d0, color = :grey, lw = 3, label = "")
128+
129+
# # Urban population
130+
# pl[:Lu] = @df d plot(:year, :Lu ./ normalizers.Lu, group = :region, color = cols, label = labs, linestyle = styles, linewidth = widths, size = panelsizef(npanels = 3))
131+
# # add aggregate
132+
# plot!(pl[:Lu], agg_city.year, agg_city.Lu ./ normalizers.Lu, color = :grey, lw = 3, label = "")
133+
134+
135+
# # agg
136+
# plot!(pl[:cityarea], agg_city.year, agg_city.cityarea ./ normalizers.cityarea, color = :grey, lw = 3, label = "")
137+
138+
# # rural rent and urban rent
139+
# pl[:ruralrents] = @df d plot(:year, :rural_rent, group = :region, color = cols, label = labs, linestyle = styles, linewidth = widths, size = panelsizef(npanels = 3))
140+
# plot!(pl[:ruralrents],agg_city.year, agg_city.rural_rent, color = :grey, lw = 3, label = "")
141+
142+
143+
# # Rural Population
144+
# pl[:Lr] = @df d plot(:year, :Lr, group = :region, color = cols, label = labs, linestyle = styles, linewidth = widths, size = panelsizef(npanels = 3))
145+
# # add aggregate
146+
# plot!(pl[:Lr], agg_city.year, agg_city.Lr, color = :grey, lw = 3, label = "")
147+
148+
# # spending shares - only aggregate
149+
# pl[:spending] = @df agg_city plot(:year, [:rshare, :ushare, :hshare], color = [:darkgreen :firebrick :darkblue], linestyle = [:solid :dot :dashdot], legend = :topleft, label = ["Rural Good" "Urban Good" "Housing"],ylims = (0,0.9))
150+
151+
# # food price - only aggregate
152+
# pl[:pr] = @df agg_city plot(:year, :pr, color = :darkgreen, lw = 3, leg = false)
153+
pl
154+
end

notebooks/makie/data.jld2

1.13 MB
Binary file not shown.

0 commit comments

Comments
 (0)