Grouped bar plots
# Prepare data
grouped_bar_df = (
gapminder[[
'country', 'year', 'lifeExp'
]].query(' year == 1967 | year == 2007 ')
.pivot_table(
index=['country'], columns='year',
values='lifeExp')
.assign(gap=lambda x: x[2007] - x[1967])
.nlargest(5, 'gap')
.reset_index()
.melt(value_vars=[1967, 2007],
id_vars=['country', 'gap'],
value_name='lifeExp')
)
grouped_bar_df
# plotnine
grouped_bars_ggplot = (ggplot(grouped_bar_df,
aes(x='country',
y='lifeExp',
fill='year')) +
geom_bar(stat="identity", position="dodge") +
geom_hline(yintercept=0, size=1, colour="#333333") +
bbc_style() +
scale_fill_manual(values=("#1380A1", "#FAAB18")) +
# ggtitle('123')+\
labs(title="We're living longer",
subtitle="Biggest life expectancy rise, 1967-2007"))
grouped_bars_ggplot
# altair
grouped_bars_altair = (
alt.Chart(grouped_bar_df)
.mark_bar(size=42)
.encode(x='year:N',
y='lifeExp:Q',
color=alt.Color('year:N', scale=alt.Scale(
range=["#1380A1", "#FAAB18"])),
column='country:N')
.properties(title={'text': "We're living longer",
'subtitle': 'Biggest life expectancy rise, 1967-2007'},
width=100
).configure_facet(
spacing=0.5,
# strokeWidth=1.0,
).configure_scale(
bandPaddingInner=0.4,
bandPaddingOuter=0.4
).configure_header(labelOrient='bottom',
labelPadding=6,
titleOrient='bottom')
.configure_axisX(
ticks=False,
labels=False,
title=None,
).configure_view(
stroke=None
)
)
grouped_bars_altair