Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v5
with:
python-version: "3.10"
Expand Down
10 changes: 10 additions & 0 deletions .github/workflows/ci-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
Expand All @@ -44,6 +46,8 @@ jobs:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
Expand All @@ -59,6 +63,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
Expand All @@ -83,6 +89,8 @@ jobs:

#
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v5
with:
Expand All @@ -108,6 +116,8 @@ jobs:
if: github.event_name == 'release'
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-python@v2
with:
python-version: "3.10"
Expand Down
196 changes: 196 additions & 0 deletions docs/examples/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ format:
html:
code-fold: true
code-summary: "Show the Code"
toc: false
---

:::::: {.column-page}
Expand Down Expand Up @@ -419,3 +420,198 @@ coffee_table
:::::
::::::

## Ecosystem

:::::: {.column-page}
::::: {.grid}

:::{.g-col-lg-6 .g-col-12 .shrink-example}

<a
href="https://posit-dev.github.io/gt-extras/examples/with-code.html#gini_coefficient_table"
target="_blank"
>See the code (gt-extras site) ⬀</a>


```{python}
# | echo: false
import polars as pl
from great_tables import GT, html
import gt_extras as gte

pre_tax_col = "gini_market__age_total"
post_tax_col = "gini_disposable__age_total"

# Read the data
df = pl.read_csv(
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-08-05/income_inequality_raw.csv",
schema={
"Entity": pl.String,
"Code": pl.String,
"Year": pl.Int64,
post_tax_col: pl.Float64,
pre_tax_col: pl.Float64,
"population_historical": pl.Int64,
"owid_region": pl.String,
},
null_values=["NA", ""],
)

# Propogate the region field to all rows of that country
df = (
df.sort("Entity")
.group_by("Entity", maintain_order=True)
.agg(
[
pl.col("Code"),
pl.col("Year"),
pl.col(post_tax_col),
pl.col(pre_tax_col),
pl.col("population_historical"),
# Most important action happens here
pl.col("owid_region").fill_null(strategy="backward"),
]
)
.explode(
[
"Code",
"Year",
post_tax_col,
pre_tax_col,
"population_historical",
"owid_region",
]
)
)

# Drop rows where there is a null in either pre-tax or post-tax cols
df = df.drop_nulls(
subset=(
pl.col(post_tax_col),
pl.col(pre_tax_col),
)
)

# Compute the percent reduction in gini coefficient.
df = df.with_columns(
((pl.col(pre_tax_col) - pl.col(post_tax_col)) / pl.col(pre_tax_col) * 100)
.round(2)
.alias("gini_pct_change")
)

# Calculate 5-year benchmark (mean) of percent change for each country
df = df.with_columns(
pl.col("gini_pct_change")
.rolling_mean(window_size=5)
.over(pl.col("Entity"))
.alias("gini_pct_benchmark_5yr")
)

# Select rows with large population in the year 2020, sorted by coefficient post-tax
df = (
# Choose a smaller pop to include more countries
df.filter(pl.col("population_historical").gt(40000000))
.filter(pl.col("Year").eq(2020))
.sort(by=pl.col(post_tax_col))
)


# Scale population
df = df.with_columns((pl.col("population_historical").log10()).alias("pop_log"))
pop_min = df["pop_log"].min() / 1
pop_max = df["pop_log"].max()

# Set up gt-extras icons, scaling population to 1-10 range
df = df.with_columns(
((pl.col("pop_log") - pop_min) / (pop_max - pop_min) * 10 + 1)
.round(0)
.cast(pl.Int64)
.alias("pop_icons")
)

# Format original population value with commas
df = df.with_columns(
pl.col("population_historical").map_elements(
lambda x: f"{int(x):,}" if x is not None else None, return_dtype=pl.String
)
)

# Apply gte.fa_icon_repeat to each entry in the pop_icons column
df_with_icons = df.with_columns(
pl.col("pop_icons").map_elements(
lambda x: gte.fa_icon_repeat(name="person", repeats=int(x)),
return_dtype=pl.String,
)
)

# Generate the table, before gt-extras add-ons
gt = (
GT(df_with_icons, rowname_col="Entity", groupname_col="owid_region")
.tab_header(
"Income Inequality Before and After Taxes in 2020",
"As measured by the Gini coefficient, where 0 is best and 1 is worst",
)
.cols_move("pop_icons", after=pre_tax_col)
.cols_align("left")
.cols_hide(["Year", "pop_log", "population_historical"])
.fmt_flag("Code")
.cols_label(
{
"Code": "",
"gini_pct_change": "Improvement Post Taxes",
"pop_icons": "Population",
}
)
.tab_source_note(
html(
"""
<div>
<strong>Source:</strong> Data from <a href="https://github.com/rfordatascience/tidytuesday">#TidyTuesday</a> (2025-08-05).<br>
<div>
<strong>Dumbbell plot:</strong>
<span style="color:#106ea0;">Blue:</span> post-tax Gini coefficient
<span style="color:#e0b165;">Gold:</span> pre-tax Gini coefficient
<br>
</div>
<strong>Bullet plot:</strong> Percent reduction in Gini after taxes for each country, compared to its 5-year average benchmark.
</div>
"""
)
)
)

# Apply the gt-extras functions via pipe
(
gt.pipe(
gte.gt_plt_dumbbell,
col1=pre_tax_col,
col2=post_tax_col,
col1_color="#e0b165",
col2_color="#106ea0",
dot_border_color="transparent",
num_decimals=2,
width=240,
label="Pre-tax to Post-tax Coefficient",
)
.pipe(
gte.gt_plt_bullet,
"gini_pct_change",
"gini_pct_benchmark_5yr",
fill="#963d4c",
target_color="#3D3D3D",
bar_height=15,
width=200,
)
.pipe(
gte.gt_merge_stack,
col1="pop_icons",
col2="population_historical",
)
.pipe(gte.gt_theme_guardian)
)
```

:::

:::::
::::::
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ dev-no-pandas = [
"jupyter",
"quartodoc>=0.11.0; python_version >= '3.9'",
"griffe==0.38.1",
"gt-extras>=0.0.7",
"polars",
"pre-commit==2.15.0",
"pyarrow",
Expand Down
Loading