Skip to content

Commit eacf742

Browse files
committed
add compose section
2 parents 7013f59 + 5b904b8 commit eacf742

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

02_develop_visualization.qmd

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ But it still didn't really work out the way I hoped, so I stuck with a vertical
619619
legend_key_width=10,
620620
legend_key_height=10,
621621
legend_text=element_text(size=8),
622+
plot_background=element_rect(fill=COLOR_BACKGROUND, color=COLOR_BACKGROUND),
622623
)
623624
+ scale_fill_manual(values=color_mapping, breaks=order, labels=legend_labels)
624625
+ guides(fill=guide_legend(title="% Change From Previous Month", nrow=1))
@@ -627,3 +628,82 @@ But it still didn't really work out the way I hoped, so I stuck with a vertical
627628

628629
## Composing in plotnine is not like R's patchwork
629630

631+
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 thought the behaviour would be similar.
632+
633+
One discrepancy is that there is no way to specify the relative size of component plots. But this might be addressed very soon [#980](https://github.com/has2k1/plotnine/pull/980)
634+
635+
It is possible to (rather labourously) pad plots by using `plot_spacer`s, which I attemp unsuccessfully below:
636+
637+
```{python}
638+
line_plot = (
639+
ggplot(
640+
labour_processed_cutted.filter(
641+
pl.col("YEAR") >= FILTER_YEAR[0],
642+
pl.col("YEAR") <= FILTER_YEAR[1],
643+
pl.col("Industry").is_in(["Total employed, all industries"]),
644+
),
645+
aes(x="DATE_YMD", y="VALUE"),
646+
)
647+
+ geom_line(color="black")
648+
+ theme_tufte()
649+
+ theme(
650+
legend_position="none",
651+
plot_title=element_text(size=10, ha="left"),
652+
axis_ticks_length=3,
653+
axis_ticks_major_y=element_line(),
654+
axis_text_y=element_text(size=8, margin={"r": 2, "l": 2, "units": "pt"}),
655+
plot_background=element_rect(fill=COLOR_BACKGROUND, color=COLOR_BACKGROUND),
656+
)
657+
+ scale_y_continuous(
658+
breaks=mb.breaks_extended(3),
659+
labels=lambda x: ["{:.0f}K".format(xi / 1000) for xi in x],
660+
)
661+
+ labs(title="Employment Rate")
662+
)
663+
664+
from plotnine.composition import Stack, plot_spacer
665+
666+
p1 = Stack(
667+
[
668+
line_plot + scale_x_datetime(expand=(0, 0)),
669+
plot_spacer(),
670+
plot_spacer(),
671+
plot_spacer(),
672+
]
673+
)
674+
675+
p2 = (
676+
plot_highlight_industry
677+
+ theme(plot_title=element_blank(), plot_subtitle=element_blank())
678+
+ scale_x_datetime(expand=(0, 0))
679+
)
680+
681+
Stack([p1, p2]) & scale_x_datetime(expand=(0, 0)) & theme_bw() & theme(
682+
plot_background=element_rect(fill=COLOR_BACKGROUND, color=COLOR_BACKGROUND)
683+
)
684+
```
685+
686+
The x axes don't automatically line up
687+
688+
This can be fixed by ensuring `expand` and the `limits` is the same:
689+
690+
```{python}
691+
Stack([plot_highlight_industry, line_plot]) & scale_x_datetime(
692+
expand=(0, 0)
693+
) & theme_bw() & theme(
694+
plot_background=element_rect(fill=COLOR_BACKGROUND, color=COLOR_BACKGROUND)
695+
)
696+
```
697+
698+
699+
But if we add `plot_spacer()`s then it won't line up:
700+
701+
```{python}
702+
Stack([plot_highlight_industry, p1]) & scale_x_datetime(
703+
expand=(0, 0)
704+
) & theme_bw() & theme(
705+
plot_background=element_rect(fill=COLOR_BACKGROUND, color=COLOR_BACKGROUND)
706+
)
707+
```
708+
709+
Possibly there are some complexities that I don't fully understand [#959](https://github.com/has2k1/plotnine/issues/959)

0 commit comments

Comments
 (0)