Skip to content

Commit 5b904b8

Browse files
committed
try line of unemployment
1 parent 4d0cb72 commit 5b904b8

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

02_develop_visualization.qmd

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import polars.selectors as cs
4848
4949
# Visualization
5050
from plotnine import *
51+
from plotnine.composition import plot_spacer, Beside, Stack
5152
5253
# Mizani helps customize the text and breaks on axes
5354
from mizani.bounds import squish
@@ -394,17 +395,72 @@ plot_data_subsetted = labour_processed_cutted.filter( # <2>
394395
pl.col('Industry') == INDUSTRY
395396
)
396397
397-
(
398+
plot_highlight = (
398399
plot
399400
+ geom_point(data=plot_data_subsetted, color='black', fill='black') # <3>
400401
)
402+
plot_highlight
401403
```
402404

403405
1. Specify indsutry
404406
2. Subset data
405407
3. Add the subsetted data to another `geom_point` layer
406408

407-
# Line plot of unemployment
409+
## Aligning multiple plots along their shared axis
410+
411+
I wanted to add a line plot of employment numbers to the heatmap. Given the similar syntax in plotnine's [compose](https://plotnine.org/guide/plot-composition.html) to R's [patchwork](https://patchwork.data-imaginist.com/), I assumed that alignment of multiple plots would happen automatically. But they don't.
412+
413+
414+
```{python}
415+
line_plot = (
416+
ggplot(
417+
labour_processed_cutted.filter(
418+
pl.col("YEAR") >= FILTER_YEAR[0],
419+
pl.col("YEAR") <= FILTER_YEAR[1],
420+
pl.col("Industry").is_in(["Total employed, all industries"]),
421+
),
422+
aes(x="DATE_YMD", y="VALUE"),
423+
)
424+
+ geom_line(color = 'black')
425+
+ theme_tufte()
426+
+ theme(
427+
legend_position="none",
428+
plot_title=element_text(size=10, ha='left'),
429+
axis_ticks_length=3,
430+
axis_ticks_major_y=element_line(),
431+
axis_text_y=element_text(
432+
size=8, margin={"r": 2, "l": 2, "units": "pt"}),
433+
)
434+
+ scale_y_continuous(
435+
breaks=mb.breaks_extended(3),
436+
labels=lambda x: ['{:.0f}K'.format(xi / 1000) for xi in x])
437+
+ labs(title='Employment Rate')
438+
)
439+
line_plot
440+
```
441+
442+
```{python}
443+
p1 = Stack([
444+
line_plot + scale_x_datetime(expand=(0, 0)),
445+
plot_spacer(), plot_spacer(), plot_spacer()
446+
])
447+
448+
p2 = (
449+
plot_highlight
450+
+ theme(plot_title=element_blank(), plot_subtitle=element_blank())
451+
+ scale_x_datetime(expand=(0, 0))
452+
)
453+
454+
Stack([p1, p2]) & scale_x_datetime(expand=(0, 0)) + theme_bw()
455+
Stack([p2, p1]) & scale_x_datetime(expand=(0, 0)) + theme_bw()
456+
457+
```
458+
459+
```{python}
460+
Stack([plot_highlight, line_plot]) & scale_x_datetime(
461+
expand=(0, 0)) + theme_bw()
462+
```
463+
408464

409465
# Appendix
410466

@@ -422,7 +478,7 @@ Initially I wanted a horizontal legend for the colors. But in order to remove th
422478
But it still didn't really work out the way I hoped, so I stuck with a vertical legend instead.
423479

424480
```{python}
425-
#| echo: false
481+
# | echo: false
426482
(
427483
ggplot(
428484
labour_processed_cutted.filter(

0 commit comments

Comments
 (0)