Examples
These are the tables of the Great Tables Examples page, from a small table of the largest landmasses to a year of sales of coffee equipment with icons and bar plots.
import std/streams
import nanuq
import tabelline, tabelline/nanuq, gtdata
let islandsMini = readCsv(newStringStream(islandsCsv))
.sort("size", descending = true)
.head(10)
let islandsTbl = tabella(islandsMini):
tabHeader "Large Landmasses of the World", "The top ten largest are presented"
tabStub "name"
tabSourceNote "Source: The World Almanac and Book of Facts, 1975, page 406."
tabSourceNote md"Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley."
tabStubhead "landmass"
fmtInteger "size"
nbTable islandsTbl
| Large Landmasses of the World | |
|---|---|
| The top ten largest are presented | |
| landmass | size |
| Asia | 16,988 |
| Africa | 11,506 |
| North America | 9,390 |
| South America | 6,795 |
| Antarctica | 5,500 |
| Europe | 3,745 |
| Australia | 2,968 |
| Greenland | 840 |
| New Guinea | 306 |
| Borneo | 280 |
| Source: The World Almanac and Book of Facts, 1975, page 406. | |
| Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley. | |
import polars as pl
from great_tables import GT, md, html
from great_tables.data import islands
islands_mini = (
pl.from_pandas(islands).sort("size", descending=True)
.head(10)
)
(
GT(islands_mini)
.tab_header(
title="Large Landmasses of the World",
subtitle="The top ten largest are presented"
)
.tab_stub(rowname_col="name")
.tab_source_note(source_note="Source: The World Almanac and Book of Facts, 1975, page 406.")
.tab_source_note(
source_note=md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
)
.tab_stubhead(label="landmass")
.fmt_integer(columns="size")
)| Large Landmasses of the World | |
| The top ten largest are presented | |
| landmass | size |
|---|---|
| Asia | 16,988 |
| Africa | 11,506 |
| North America | 9,390 |
| South America | 6,795 |
| Antarctica | 5,500 |
| Europe | 3,745 |
| Australia | 2,968 |
| Greenland | 840 |
| New Guinea | 306 |
| Borneo | 280 |
| Source: The World Almanac and Book of Facts, 1975, page 406. | |
| Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley. | |
library(gt)
library(dplyr)
library(tidyr)
islands_mini <-
tibble(name = names(islands), size = islands) |>
arrange(desc(size)) |>
slice_head(n = 10)
islands_mini |>
gt(rowname_col = "name") |>
tab_header(
title = "Large Landmasses of the World",
subtitle = "The top ten largest are presented"
) |>
tab_source_note(source_note = "Source: The World Almanac and Book of Facts, 1975, page 406.") |>
tab_source_note(
source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
) |>
tab_stubhead(label = "landmass") |>
fmt_integer(columns = size)| Large Landmasses of the World | |
| The top ten largest are presented | |
| landmass | size |
|---|---|
| Asia | 16,988 |
| Africa | 11,506 |
| North America | 9,390 |
| South America | 6,795 |
| Antarctica | 5,500 |
| Europe | 3,745 |
| Australia | 2,968 |
| Greenland | 840 |
| New Guinea | 306 |
| Borneo | 280 |
| Source: The World Almanac and Book of Facts, 1975, page 406. | |
| Reference: McNeil, D. R. (1977) Interactive Data Analysis. Wiley. | |
let airqualityMini = readCsv(newStringStream(airqualityCsv), inferSchemaLength = 10_000)
.head(10)
.withColumns(lit(1973).alias("Year"))
let airqualityTbl = tabella(airqualityMini):
tabHeader "New York Air Quality Measurements",
"Daily measurements in New York City (May 1-10, 1973)"
tabSpanner "Time", ["Year", "Month", "Day"]
tabSpanner "Measurement", ["Ozone", "Solar_R", "Wind", "Temp"]
colsMoveToStart ["Year", "Month", "Day"]
colsLabel:
Ozone: html"Ozone,<br>ppbV"
Solar_R: html"Solar R.,<br>cal/m<sup>2</sup>"
Wind: html"Wind,<br>mph"
Temp: html"Temp,<br>°F"
nbTable airqualityTbl
| New York Air Quality Measurements | ||||||
|---|---|---|---|---|---|---|
| Daily measurements in New York City (May 1-10, 1973) | ||||||
| Time | Measurement | |||||
| Year | Month | Day | Ozone, ppbV | Solar R., cal/m2 | Wind, mph | Temp, °F |
| 1973 | 5 | 1 | 41 | 190 | 7.4 | 67 |
| 1973 | 5 | 2 | 36 | 118 | 8.0 | 72 |
| 1973 | 5 | 3 | 12 | 149 | 12.6 | 74 |
| 1973 | 5 | 4 | 18 | 313 | 11.5 | 62 |
| 1973 | 5 | 5 | NA | NA | 14.3 | 56 |
| 1973 | 5 | 6 | 28 | NA | 14.9 | 66 |
| 1973 | 5 | 7 | 23 | 299 | 8.6 | 65 |
| 1973 | 5 | 8 | 19 | 99 | 13.8 | 59 |
| 1973 | 5 | 9 | 8 | 19 | 20.1 | 61 |
| 1973 | 5 | 10 | NA | 194 | 8.6 | 69 |
from great_tables import GT, html
from great_tables.data import airquality
airquality_mini = airquality.head(10).assign(Year = 1973)
(
GT(airquality_mini)
.tab_header(
title="New York Air Quality Measurements",
subtitle="Daily measurements in New York City (May 1-10, 1973)"
)
.tab_spanner(
label="Time",
columns=["Year", "Month", "Day"]
)
.tab_spanner(
label="Measurement",
columns=["Ozone", "Solar_R", "Wind", "Temp"]
)
.cols_move_to_start(columns=["Year", "Month", "Day"])
.cols_label(
Ozone = html("Ozone,<br>ppbV"),
Solar_R = html("Solar R.,<br>cal/m<sup>2</sup>"),
Wind = html("Wind,<br>mph"),
Temp = html("Temp,<br>°F")
)
)| New York Air Quality Measurements | ||||||
| Daily measurements in New York City (May 1-10, 1973) | ||||||
| Time | Measurement | |||||
|---|---|---|---|---|---|---|
| Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
| 1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
| 1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
| 1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
| 1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
| 1973 | 5 | 5 | None | None | 14.3 | 56 |
| 1973 | 5 | 6 | 28.0 | None | 14.9 | 66 |
| 1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
| 1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
| 1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
| 1973 | 5 | 10 | None | 194.0 | 8.6 | 69 |
airquality_mini <- airquality |> head(10) |> mutate(Year = 1973L)
airquality_mini |>
gt() |>
tab_header(
title = "New York Air Quality Measurements",
subtitle = "Daily measurements in New York City (May 1-10, 1973)"
) |>
tab_spanner(
label = "Time",
columns = c(Year, Month, Day)
) |>
tab_spanner(
label = "Measurement",
columns = c(Ozone, Solar.R, Wind, Temp)
) |>
cols_move_to_start(columns = c(Year, Month, Day)) |>
cols_label(
Ozone = html("Ozone,<br>ppbV"),
Solar.R = html("Solar R.,<br>cal/m<sup>2</sup>"),
Wind = html("Wind,<br>mph"),
Temp = html("Temp,<br>°F")
)| New York Air Quality Measurements | ||||||
| Daily measurements in New York City (May 1-10, 1973) | ||||||
|
Time
|
Measurement
|
|||||
|---|---|---|---|---|---|---|
| Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
| 1973 | 5 | 1 | 41 | 190 | 7.4 | 67 |
| 1973 | 5 | 2 | 36 | 118 | 8.0 | 72 |
| 1973 | 5 | 3 | 12 | 149 | 12.6 | 74 |
| 1973 | 5 | 4 | 18 | 313 | 11.5 | 62 |
| 1973 | 5 | 5 | NA | NA | 14.3 | 56 |
| 1973 | 5 | 6 | 28 | NA | 14.9 | 66 |
| 1973 | 5 | 7 | 23 | 299 | 8.6 | 65 |
| 1973 | 5 | 8 | 19 | 99 | 13.8 | 59 |
| 1973 | 5 | 9 | 8 | 19 | 20.1 | 61 |
| 1973 | 5 | 10 | NA | 194 | 8.6 | 69 |
import std/[sequtils, tables]
# Get lists of 2-letter country codes for each region of Oceania
let oceania = {
"Australasia": @["AU", "NZ"],
"Melanesia": @["NC", "PG", "SB", "VU"],
"Micronesia": @["FM", "GU", "KI", "MH", "MP", "NR", "PW"],
"Polynesia": @["PF", "WS", "TO", "TV"]}.toOrderedTable
# Create a table mapping country to region (e.g. AU -> Australasia)
var countryToRegion = initOrderedTable[string, string]()
for region, countries in oceania:
for country in countries: countryToRegion[country] = region
var codes: seq[string]
for country in countryToRegion.keys: codes.add country
let widePops = readCsv(newStringStream(countrypopsCsv), inferSchemaLength = 10_000)
.filter(col("country_code_2").isIn(codes) and col("year").isIn([2000, 2010, 2020]))
.withColumns(col("country_code_2").replace(countryToRegion.pairs.toSeq).alias("region"))
.pivot(index = ["country_name", "region"], on = "year", values = "population")
.sort("2020", descending = true)
let widePopsTbl = tabella(widePops, rownameCol = "country_name", groupnameCol = "region"):
tabHeader "Populations of Oceania's Countries in 2000, 2010, and 2020"
tabSpanner "Total Population", everything()
fmtInteger()
nbTable widePopsTbl
| Populations of Oceania's Countries in 2000, 2010, and 2020 | |||
|---|---|---|---|
| Total Population | |||
| 2000 | 2010 | 2020 | |
| Australasia | |||
| Australia | 19,028,802 | 22,031,750 | 25,655,289 |
| New Zealand | 3,857,700 | 4,350,700 | 5,090,200 |
| Melanesia | |||
| Papua New Guinea | 5,508,297 | 7,583,269 | 9,749,640 |
| Solomon Islands | 429,978 | 540,394 | 691,191 |
| Vanuatu | 192,074 | 245,453 | 311,685 |
| New Caledonia | 213,230 | 249,750 | 272,460 |
| Polynesia | |||
| French Polynesia | 250,927 | 283,788 | 301,920 |
| Samoa | 184,008 | 194,672 | 214,929 |
| Tonga | 102,603 | 107,383 | 105,254 |
| Tuvalu | 9,638 | 10,550 | 11,069 |
| Micronesia | |||
| Guam | 160,188 | 164,905 | 169,231 |
| Kiribati | 88,826 | 107,995 | 126,463 |
| Micronesia (Federated States) | 111,709 | 107,588 | 112,106 |
| Northern Mariana Islands | 80,338 | 54,087 | 49,587 |
| Marshall Islands | 54,224 | 53,416 | 43,413 |
| Palau | 19,726 | 18,540 | 17,972 |
| Nauru | 10,377 | 10,241 | 12,315 |
from great_tables import GT
from great_tables.data import countrypops
import polars as pl
import polars.selectors as cs
# Get vectors of 2-letter country codes for each region of Oceania
oceania = {
"Australasia": ["AU", "NZ"],
"Melanesia": ["NC", "PG", "SB", "VU"],
"Micronesia": ["FM", "GU", "KI", "MH", "MP", "NR", "PW"],
"Polynesia": ["PF", "WS", "TO", "TV"],
}
# Create a dictionary mapping country to region (e.g. AU -> Australasia)
country_to_region = {
country: region for region, countries in oceania.items() for country in countries
}
wide_pops = (
pl.from_pandas(countrypops)
.filter(
pl.col("country_code_2").is_in(list(country_to_region))
& pl.col("year").is_in([2000, 2010, 2020])
)
.with_columns(pl.col("country_code_2").replace(country_to_region).alias("region"))
.pivot(index=["country_name", "region"], on="year", values="population")
.sort("2020", descending=True)
)
(
GT(wide_pops)
.tab_header(title="Populations of Oceania's Countries in 2000, 2010, and 2020")
.tab_spanner(label="Total Population", columns=cs.all())
.tab_stub(rowname_col="country_name", groupname_col="region")
.fmt_integer()
)| Populations of Oceania's Countries in 2000, 2010, and 2020 | |||
| Total Population | |||
|---|---|---|---|
| 2000 | 2010 | 2020 | |
| Australasia | |||
| Australia | 19,028,802 | 22,031,750 | 25,655,289 |
| New Zealand | 3,857,700 | 4,350,700 | 5,090,200 |
| Melanesia | |||
| Papua New Guinea | 5,508,297 | 7,583,269 | 9,749,640 |
| Solomon Islands | 429,978 | 540,394 | 691,191 |
| Vanuatu | 192,074 | 245,453 | 311,685 |
| New Caledonia | 213,230 | 249,750 | 272,460 |
| Polynesia | |||
| French Polynesia | 250,927 | 283,788 | 301,920 |
| Samoa | 184,008 | 194,672 | 214,929 |
| Tonga | 102,603 | 107,383 | 105,254 |
| Tuvalu | 9,638 | 10,550 | 11,069 |
| Micronesia | |||
| Guam | 160,188 | 164,905 | 169,231 |
| Kiribati | 88,826 | 107,995 | 126,463 |
| Micronesia (Federated States) | 111,709 | 107,588 | 112,106 |
| Northern Mariana Islands | 80,338 | 54,087 | 49,587 |
| Marshall Islands | 54,224 | 53,416 | 43,413 |
| Palau | 19,726 | 18,540 | 17,972 |
| Nauru | 10,377 | 10,241 | 12,315 |
# Get vectors of 2-letter country codes for each region of Oceania
oceania <- list(
Australasia = c("AU", "NZ"),
Melanesia = c("NC", "PG", "SB", "VU"),
Micronesia = c("FM", "GU", "KI", "MH", "MP", "NR", "PW"),
Polynesia = c("PF", "WS", "TO", "TV")
)
# A named vector mapping country to region (e.g. AU -> Australasia)
country_to_region <- setNames(rep(names(oceania), lengths(oceania)), unlist(oceania))
wide_pops <-
countrypops |>
filter(country_code_2 %in% names(country_to_region), year %in% c(2000, 2010, 2020)) |>
mutate(region = country_to_region[country_code_2]) |>
select(country_name, region, year, population) |>
pivot_wider(names_from = year, values_from = population) |>
arrange(desc(`2020`))
wide_pops |>
gt(rowname_col = "country_name", groupname_col = "region") |>
tab_header(title = "Populations of Oceania's Countries in 2000, 2010, and 2020") |>
tab_spanner(label = "Total Population", columns = everything()) |>
fmt_integer()| Populations of Oceania's Countries in 2000, 2010, and 2020 | |||
|
Total Population
|
|||
|---|---|---|---|
| 2000 | 2010 | 2020 | |
| Australasia | |||
| Australia | 19,028,802 | 22,031,750 | 25,649,248 |
| New Zealand | 3,857,700 | 4,350,700 | 5,090,200 |
| Melanesia | |||
| Papua New Guinea | 5,537,064 | 7,633,523 | 9,815,746 |
| Solomon Islands | 440,469 | 532,968 | 744,498 |
| Vanuatu | 186,281 | 238,248 | 298,858 |
| New Caledonia | 223,098 | 261,582 | 284,176 |
| Polynesia | |||
| French Polynesia | 240,337 | 270,580 | 279,209 |
| Samoa | 181,685 | 193,023 | 211,944 |
| Tonga | 102,677 | 107,400 | 105,704 |
| Tuvalu | 9,544 | 10,614 | 10,399 |
| Micronesia | |||
| Guam | 160,065 | 166,522 | 162,158 |
| Kiribati | 88,613 | 108,704 | 126,099 |
| Micronesia | 112,044 | 107,540 | 110,916 |
| Northern Mariana Islands | 68,356 | 54,091 | 47,528 |
| Marshall Islands | 50,706 | 52,044 | 42,706 |
| Palau | 19,178 | 18,396 | 17,792 |
| Nauru | 10,168 | 10,043 | 11,643 |
let townyMini = readCsv(newStringStream(townyCsv), inferSchemaLength = 10_000)
.select("name", "website", "density_2021", "land_area_km2", "latitude", "longitude")
.sort("density_2021", descending = true)
.head(10)
.withColumns(
concatStr([lit("["), col("name"), lit("]("), col("website"), lit(")")]).alias("url_name"),
concatStr([lit("[map](http://maps.google.com/?ie=UTF8&hq=&ll="),
col("latitude").`cast`(String), lit(","), col("longitude").`cast`(String),
lit("&z=13)")]).alias("location"))
.select("url_name", "location", "land_area_km2", "density_2021")
let townyTbl = tabella(townyMini, rownameCol = "url_name"):
tabHeader "The Municipalities of Ontario", "The top 10 highest population density in 2021"
tabStubhead "Municipality"
fmtMarkdown ["url_name", "location"]
fmtNumber ["land_area_km2", "density_2021"], decimals = 1
colsLabel:
land_area_km2: html"land area, <br>km<sup>2</sup>"
density_2021: html"density, <br>people/km<sup>2</sup>"
nbTable townyTbl
| The Municipalities of Ontario | |||
|---|---|---|---|
| The top 10 highest population density in 2021 | |||
| Municipality | location | land area, km2 | density, people/km2 |
| Toronto | map | 631.1 | 4,427.8 |
| Brampton | map | 265.9 | 2,469.0 |
| Mississauga | map | 292.7 | 2,452.6 |
| Newmarket | map | 38.5 | 2,284.2 |
| Richmond Hill | map | 100.8 | 2,004.4 |
| Orangeville | map | 15.2 | 1,989.9 |
| Ajax | map | 66.6 | 1,900.8 |
| Waterloo | map | 64.1 | 1,895.7 |
| Kitchener | map | 136.8 | 1,877.7 |
| Guelph | map | 87.4 | 1,644.1 |
from great_tables import GT, html
from great_tables.data import towny
towny_mini = (
towny[["name", "website", "density_2021", "land_area_km2", "latitude", "longitude"]]
.sort_values("density_2021", ascending=False)
.head(10)
)
towny_mini["url_name"] = "[" + towny_mini["name"] + "](" + towny_mini["website"] + ")"
towny_mini["location"] = (
"[map](http://maps.google.com/?ie=UTF8&hq=&ll="
+ towny_mini["latitude"].astype(str)
+ ","
+ towny_mini["longitude"].astype(str)
+ "&z=13)"
)
(
GT(
towny_mini[["url_name", "location", "land_area_km2", "density_2021"]],
rowname_col="url_name",
)
.tab_header(
title="The Municipalities of Ontario",
subtitle="The top 10 highest population density in 2021",
)
.tab_stubhead(label="Municipality")
.fmt_markdown(columns=["url_name", "location"])
.fmt_number(columns=["land_area_km2", "density_2021"], decimals=1)
.cols_label(
land_area_km2=html("land area, <br>km<sup>2</sup>"),
density_2021=html("density, <br>people/km<sup>2</sup>"),
)
)| The Municipalities of Ontario | |||
| The top 10 highest population density in 2021 | |||
| Municipality | location | land area, km2 |
density, people/km2 |
|---|---|---|---|
| Toronto | map | 631.1 | 4,427.8 |
| Brampton | map | 265.9 | 2,469.0 |
| Mississauga | map | 292.7 | 2,452.6 |
| Newmarket | map | 38.5 | 2,284.2 |
| Richmond Hill | map | 100.8 | 2,004.4 |
| Orangeville | map | 15.2 | 1,989.9 |
| Ajax | map | 66.6 | 1,900.8 |
| Waterloo | map | 64.1 | 1,895.7 |
| Kitchener | map | 136.8 | 1,877.7 |
| Guelph | map | 87.4 | 1,644.1 |
towny_mini <-
towny |>
select(name, website, density_2021, land_area_km2, latitude, longitude) |>
arrange(desc(density_2021)) |>
slice_head(n = 10) |>
mutate(
url_name = paste0("[", name, "](", website, ")"),
location = paste0(
"[map](http://maps.google.com/?ie=UTF8&hq=&ll=",
latitude, ",", longitude, "&z=13)"
)
)
towny_mini |>
select(url_name, location, land_area_km2, density_2021) |>
gt(rowname_col = "url_name") |>
tab_header(
title = "The Municipalities of Ontario",
subtitle = "The top 10 highest population density in 2021"
) |>
tab_stubhead(label = "Municipality") |>
fmt_markdown(columns = c(url_name, location)) |>
fmt_number(columns = c(land_area_km2, density_2021), decimals = 1) |>
cols_label(
land_area_km2 = html("land area, <br>km<sup>2</sup>"),
density_2021 = html("density, <br>people/km<sup>2</sup>")
)| The Municipalities of Ontario | |||
| The top 10 highest population density in 2021 | |||
| Municipality | location | land area, km2 |
density, people/km2 |
|---|---|---|---|
| Toronto | map | 631.1 | 4,427.8 |
| Brampton | map | 265.9 | 2,469.0 |
| Mississauga | map | 292.7 | 2,452.6 |
| Newmarket | map | 38.5 | 2,284.2 |
| Richmond Hill | map | 100.8 | 2,004.4 |
| Orangeville | map | 15.2 | 1,989.9 |
| Ajax | map | 66.6 | 1,900.8 |
| Waterloo | map | 64.1 | 1,895.7 |
| Kitchener | map | 136.8 | 1,877.7 |
| Guelph | map | 87.4 | 1,644.1 |
let szaPivot = readCsv(newStringStream(szaCsv), inferSchemaLength = 0) # every column as text
.filter(col("latitude") == "20" and col("tst") <= lit("1200"))
.drop("latitude")
.dropNulls()
.withColumns(col("sza").`cast`(Float64))
.pivot(values = "sza", index = "month", on = "tst", sortColumns = true)
let szaTbl = tabella(szaPivot, rownameCol = "month"):
dataColor domain = (90.0, 0.0), palette = @["rebeccapurple", "white", "orange"],
naColor = "white"
tabHeader "Solar Zenith Angles from 05:30 to 12:00",
html"Average monthly values at latitude of 20°N."
subMissing(missingText = "")
nbTable szaTbl
| Solar Zenith Angles from 05:30 to 12:00 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Average monthly values at latitude of 20°N. | ||||||||||||||
| 0530 | 0600 | 0630 | 0700 | 0730 | 0800 | 0830 | 0900 | 0930 | 1000 | 1030 | 1100 | 1130 | 1200 | |
| jan | 84.9 | 78.7 | 72.7 | 66.1 | 61.5 | 56.5 | 52.1 | 48.3 | 45.5 | 43.6 | 43.0 | |||
| feb | 88.9 | 82.5 | 75.8 | 69.6 | 63.3 | 57.7 | 52.2 | 47.4 | 43.1 | 40.0 | 37.8 | 37.2 | ||
| mar | 85.7 | 78.8 | 72.0 | 65.2 | 58.6 | 52.3 | 46.2 | 40.5 | 35.5 | 31.4 | 28.6 | 27.7 | ||
| apr | 88.5 | 81.5 | 74.4 | 67.4 | 60.3 | 53.4 | 46.5 | 39.7 | 33.2 | 26.9 | 21.3 | 17.2 | 15.5 | |
| may | 85.0 | 78.2 | 71.2 | 64.3 | 57.2 | 50.2 | 43.2 | 36.1 | 29.1 | 26.1 | 15.2 | 8.8 | 5.0 | |
| jun | 89.2 | 82.7 | 76.0 | 69.3 | 62.5 | 55.7 | 48.8 | 41.9 | 35.0 | 28.1 | 21.1 | 14.2 | 7.3 | 2.0 |
| jul | 88.8 | 82.3 | 75.7 | 69.1 | 62.3 | 55.5 | 48.7 | 41.8 | 35.0 | 28.1 | 21.2 | 14.3 | 7.7 | 3.1 |
| aug | 83.8 | 77.1 | 70.2 | 63.3 | 56.4 | 49.4 | 42.4 | 35.4 | 28.3 | 21.3 | 14.3 | 7.3 | 1.9 | |
| sep | 87.2 | 80.2 | 73.2 | 66.1 | 59.1 | 52.1 | 45.1 | 38.1 | 31.3 | 24.7 | 18.6 | 13.7 | 11.6 | |
| oct | 84.1 | 77.1 | 70.2 | 63.3 | 56.5 | 49.9 | 43.5 | 37.5 | 32.0 | 27.4 | 24.3 | 23.1 | ||
| nov | 87.8 | 81.3 | 74.5 | 68.3 | 61.8 | 56.0 | 50.2 | 45.3 | 40.7 | 37.4 | 35.1 | 34.4 | ||
| dec | 84.3 | 78.0 | 71.8 | 66.1 | 60.5 | 55.6 | 50.9 | 47.2 | 44.2 | 42.4 | 41.8 | |||
from great_tables import GT, html
from great_tables.data import sza
import polars as pl
import polars.selectors as cs
sza_pivot = (
pl.from_pandas(sza)
.filter((pl.col("latitude") == "20") & (pl.col("tst") <= "1200"))
.select(pl.col("*").exclude("latitude"))
.drop_nulls()
.pivot(values="sza", index="month", on="tst", sort_columns=True)
)
(
GT(sza_pivot, rowname_col="month")
.data_color(
domain=[90, 0],
palette=["rebeccapurple", "white", "orange"],
na_color="white",
)
.tab_header(
title="Solar Zenith Angles from 05:30 to 12:00",
subtitle=html("Average monthly values at latitude of 20°N."),
)
.sub_missing(missing_text="")
)| Solar Zenith Angles from 05:30 to 12:00 | ||||||||||||||
| Average monthly values at latitude of 20°N. | ||||||||||||||
| 0530 | 0600 | 0630 | 0700 | 0730 | 0800 | 0830 | 0900 | 0930 | 1000 | 1030 | 1100 | 1130 | 1200 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| jan | 84.9 | 78.7 | 72.7 | 66.1 | 61.5 | 56.5 | 52.1 | 48.3 | 45.5 | 43.6 | 43.0 | |||
| feb | 88.9 | 82.5 | 75.8 | 69.6 | 63.3 | 57.7 | 52.2 | 47.4 | 43.1 | 40.0 | 37.8 | 37.2 | ||
| mar | 85.7 | 78.8 | 72.0 | 65.2 | 58.6 | 52.3 | 46.2 | 40.5 | 35.5 | 31.4 | 28.6 | 27.7 | ||
| apr | 88.5 | 81.5 | 74.4 | 67.4 | 60.3 | 53.4 | 46.5 | 39.7 | 33.2 | 26.9 | 21.3 | 17.2 | 15.5 | |
| may | 85.0 | 78.2 | 71.2 | 64.3 | 57.2 | 50.2 | 43.2 | 36.1 | 29.1 | 26.1 | 15.2 | 8.8 | 5.0 | |
| jun | 89.2 | 82.7 | 76.0 | 69.3 | 62.5 | 55.7 | 48.8 | 41.9 | 35.0 | 28.1 | 21.1 | 14.2 | 7.3 | 2.0 |
| jul | 88.8 | 82.3 | 75.7 | 69.1 | 62.3 | 55.5 | 48.7 | 41.8 | 35.0 | 28.1 | 21.2 | 14.3 | 7.7 | 3.1 |
| aug | 83.8 | 77.1 | 70.2 | 63.3 | 56.4 | 49.4 | 42.4 | 35.4 | 28.3 | 21.3 | 14.3 | 7.3 | 1.9 | |
| sep | 87.2 | 80.2 | 73.2 | 66.1 | 59.1 | 52.1 | 45.1 | 38.1 | 31.3 | 24.7 | 18.6 | 13.7 | 11.6 | |
| oct | 84.1 | 77.1 | 70.2 | 63.3 | 56.5 | 49.9 | 43.5 | 37.5 | 32.0 | 27.4 | 24.3 | 23.1 | ||
| nov | 87.8 | 81.3 | 74.5 | 68.3 | 61.8 | 56.0 | 50.2 | 45.3 | 40.7 | 37.4 | 35.1 | 34.4 | ||
| dec | 84.3 | 78.0 | 71.8 | 66.1 | 60.5 | 55.6 | 50.9 | 47.2 | 44.2 | 42.4 | 41.8 | |||
sza_pivot <-
sza |>
filter(latitude == 20, tst <= "1200") |>
select(-latitude) |>
drop_na() |>
pivot_wider(names_from = tst, values_from = sza, names_sort = TRUE)
sza_pivot |>
gt(rowname_col = "month") |>
data_color(
domain = c(0, 90),
palette = c("orange", "white", "#663399"), # R has no rebeccapurple
na_color = "white"
) |>
tab_header(
title = "Solar Zenith Angles from 05:30 to 12:00",
subtitle = html("Average monthly values at latitude of 20°N.")
) |>
sub_missing(missing_text = "")| Solar Zenith Angles from 05:30 to 12:00 | ||||||||||||||
| Average monthly values at latitude of 20°N. | ||||||||||||||
| 0530 | 0600 | 0630 | 0700 | 0730 | 0800 | 0830 | 0900 | 0930 | 1000 | 1030 | 1100 | 1130 | 1200 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| jan | 84.9 | 78.7 | 72.7 | 66.1 | 61.5 | 56.5 | 52.1 | 48.3 | 45.5 | 43.6 | 43.0 | |||
| feb | 88.9 | 82.5 | 75.8 | 69.6 | 63.3 | 57.7 | 52.2 | 47.4 | 43.1 | 40.0 | 37.8 | 37.2 | ||
| mar | 85.7 | 78.8 | 72.0 | 65.2 | 58.6 | 52.3 | 46.2 | 40.5 | 35.5 | 31.4 | 28.6 | 27.7 | ||
| apr | 88.5 | 81.5 | 74.4 | 67.4 | 60.3 | 53.4 | 46.5 | 39.7 | 33.2 | 26.9 | 21.3 | 17.2 | 15.5 | |
| may | 85.0 | 78.2 | 71.2 | 64.3 | 57.2 | 50.2 | 43.2 | 36.1 | 29.1 | 26.1 | 15.2 | 8.8 | 5.0 | |
| jun | 89.2 | 82.7 | 76.0 | 69.3 | 62.5 | 55.7 | 48.8 | 41.9 | 35.0 | 28.1 | 21.1 | 14.2 | 7.3 | 2.0 |
| jul | 88.8 | 82.3 | 75.7 | 69.1 | 62.3 | 55.5 | 48.7 | 41.8 | 35.0 | 28.1 | 21.2 | 14.3 | 7.7 | 3.1 |
| aug | 83.8 | 77.1 | 70.2 | 63.3 | 56.4 | 49.4 | 42.4 | 35.4 | 28.3 | 21.3 | 14.3 | 7.3 | 1.9 | |
| sep | 87.2 | 80.2 | 73.2 | 66.1 | 59.1 | 52.1 | 45.1 | 38.1 | 31.3 | 24.7 | 18.6 | 13.7 | 11.6 | |
| oct | 84.1 | 77.1 | 70.2 | 63.3 | 56.5 | 49.9 | 43.5 | 37.5 | 32.0 | 27.4 | 24.3 | 23.1 | ||
| nov | 87.8 | 81.3 | 74.5 | 68.3 | 61.8 | 56.0 | 50.2 | 45.3 | 40.7 | 37.4 | 35.1 | 34.4 | ||
| dec | 84.3 | 78.0 | 71.8 | 66.1 | 60.5 | 55.6 | 50.9 | 47.2 | 44.2 | 42.4 | 41.8 | |||
The Sports earnings example, the highest paid athletes of 2023, has a page of its own.
The next example is the carbon intensity of electricity consumption, by Grant Chalmers (source, blog post with the R code).
let powerCie = readCsv(newStringStream(powerCieCsv), inferSchemaLength = 10_000)
let widths = powerCie.columns[1 .. ^1].mapIt((it, "58px")) # every column but Zone
let shares = ["Hydro", "Nuclear", "Wind", "Solar", "Geothermal", "Biomass", "Gas", "Coal",
"Oil", "Unknown", "Hydro Discharge", "Battery Discharge"]
let powerTbl = tabella(powerCie):
fmtPercent shares, decimals = 1
fmtNumber "CO2 Intensity", decimals = 0
dataColor "CO2 Intensity", domain = (0.0, 900.0), palette = @["#00A600", "#E6E600",
"#E8C32E", "#D69C4E", "#Dc863B", "sienna", "sienna4", "tomato4", "brown"]
dataColor ["Hydro", "Nuclear", "Wind", "Solar", "Geothermal"], domain = (0.0, 1.0),
palette = @["snow", "chartreuse4", "chartreuse3", "#00A600"]
dataColor "Biomass", domain = (0.0, 0.3), palette = @["snow", "#EEC900", "#E8C32E", "#D69C4E"]
dataColor ["Gas", "Coal", "Oil"], domain = (0.0, 1.0),
palette = @["snow", "#Dc863B", "#D69C4E", "sienna4", "tomato4"]
dataColor ["Zone", "Unknown", "Hydro Discharge", "Battery Discharge"],
palette = @["snow", "snow", "snow", "snow"]
colsWidth widths
tabHeader md"2023 Mean **Carbon Intensity** (gCO2eq/kWh) and **Power Consumption** Breakdown (%)"
tabSourceNote md("**Source**: api.electricitymap.org" &
" | **Methodology**: https://www.electricitymaps.com/methodology." &
" Some emissions factors are based on IPCC 2014 defaults, while some are based on more accurate regional factors." &
"<br>All zones are publicly available on the *Carbon intensity and emission factors* tab via Google docs link")
optTableFont systemFonts("humanist")
tabOptions(sourceNotesFontSize = "x-small", sourceNotesPadding = "3px",
dataRowPadding = "1px", headingBackgroundColor = "antiquewhite",
sourceNotesBackgroundColor = "antiquewhite",
columnLabelsBackgroundColor = "antiquewhite", tableBackgroundColor = "snow",
dataRowPaddingHorizontal = "3px", columnLabelsPaddingHorizontal = "58px")
colsAlign alCenter
colsAlign alLeft, "Zone"
optTableOutline()
nbTable powerTbl
| 2023 Mean Carbon Intensity (gCO2eq/kWh) and Power Consumption Breakdown (%) | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Zone | CO2 Intensity | Hydro | Nuclear | Wind | Solar | Geothermal | Biomass | Gas | Coal | Oil | Unknown | Hydro Discharge | Battery Discharge |
| Sweden | 26 | 39.1% | 26.8% | 27.7% | 0.1% | 0.0% | 0.4% | 0.4% | 0.8% | 0.0% | 4.6% | 0.1% | 0.0% |
| Iceland | 28 | 69.4% | 0.0% | 0.0% | 0.0% | 30.6% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% |
| Quebec | 35 | 90.1% | 2.1% | 4.4% | 0.0% | 0.0% | 1.9% | 1.4% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% |
| France | 46 | 12.3% | 65.4% | 10.3% | 1.8% | 0.0% | 1.0% | 7.1% | 0.3% | 0.3% | 0.1% | 1.4% | 0.0% |
| Ontario | 104 | 23.3% | 49.4% | 8.7% | 0.1% | 0.0% | 0.2% | 18.1% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% |
| New Zealand | 106 | 60.5% | 0.0% | 7.7% | 0.1% | 19.0% | 0.0% | 6.8% | 3.7% | 0.0% | 2.2% | 0.0% | 0.0% |
| Finland | 107 | 20.2% | 36.5% | 24.1% | 0.1% | 0.0% | 6.2% | 3.0% | 8.1% | 0.0% | 1.8% | 0.0% | 0.0% |
| South Australia | 132 | 0.7% | 0.0% | 42.6% | 33.7% | 0.0% | 0.0% | 13.3% | 9.0% | 0.0% | 0.0% | 0.0% | 0.7% |
| Spain | 132 | 17.1% | 24.2% | 25.1% | 8.0% | 0.0% | 2.0% | 18.8% | 1.3% | 0.2% | 0.3% | 3.0% | 0.0% |
| Belgium | 147 | 1.3% | 39.6% | 25.2% | 3.6% | 0.0% | 2.8% | 19.4% | 1.7% | 0.1% | 4.9% | 1.2% | 0.0% |
| Tasmania | 162 | 49.0% | 0.0% | 22.6% | 10.8% | 0.0% | 0.0% | 1.5% | 16.1% | 0.0% | 0.0% | 0.0% | 0.0% |
| East Denmark | 184 | 6.4% | 5.5% | 48.4% | 1.3% | 0.0% | 16.8% | 7.7% | 10.8% | 1.4% | 1.4% | 0.4% | 0.0% |
| West Denmark | 188 | 8.8% | 2.2% | 56.3% | 1.6% | 0.0% | 7.6% | 8.5% | 13.0% | 0.9% | 0.4% | 0.6% | 0.0% |
| Great Britain | 214 | 3.8% | 12.4% | 35.9% | 2.7% | 0.0% | 6.2% | 35.1% | 2.0% | 0.0% | 1.0% | 1.0% | 0.0% |
| Netherlands | 218 | 1.1% | 3.9% | 46.7% | 10.8% | 0.0% | 4.6% | 22.4% | 8.6% | 0.8% | 1.1% | 0.2% | 0.0% |
| New York ISO | 275 | 23.7% | 22.8% | 4.9% | 0.0% | 0.0% | 0.1% | 46.9% | 0.0% | 0.0% | 1.6% | 0.0% | 0.0% |
| Italy (North) | 307 | 22.7% | 14.5% | 3.9% | 2.9% | 0.2% | 3.1% | 38.4% | 1.5% | 0.2% | 9.3% | 3.3% | 0.0% |
| California | 328 | 8.4% | 12.7% | 7.9% | 12.0% | 3.0% | 1.8% | 48.5% | 2.1% | 0.0% | 1.2% | 0.0% | 2.6% |
| Germany | 389 | 4.4% | 2.8% | 39.7% | 3.3% | 0.0% | 8.7% | 14.4% | 23.3% | 0.6% | 0.6% | 2.1% | 0.0% |
| Ireland | 389 | 3.7% | 0.8% | 38.5% | 0.2% | 0.0% | 2.5% | 42.4% | 9.7% | 2.0% | 0.1% | 0.1% | 0.0% |
| Western Australia | 417 | 0.0% | 0.0% | 14.1% | 33.8% | 0.0% | 0.3% | 24.2% | 27.1% | 0.3% | 0.0% | 0.0% | 0.3% |
| Texas | 432 | 0.0% | 9.1% | 22.3% | 6.0% | 0.0% | 0.0% | 46.1% | 16.1% | 0.0% | 0.4% | 0.0% | 0.0% |
| Alberta | 447 | 1.9% | 0.0% | 12.4% | 1.1% | 0.0% | 2.5% | 70.7% | 7.2% | 0.0% | 4.1% | 0.0% | 0.0% |
| Victoria | 508 | 3.9% | 0.0% | 17.5% | 19.0% | 0.0% | 0.0% | 0.3% | 59.1% | 0.0% | 0.0% | 0.0% | 0.2% |
| New South Wales | 578 | 3.2% | 0.0% | 9.5% | 23.7% | 0.0% | 0.2% | 0.7% | 62.6% | 0.0% | 0.0% | 0.0% | 0.1% |
| Queensland | 662 | 1.9% | 0.0% | 3.8% | 21.1% | 0.0% | 0.0% | 7.2% | 65.7% | 0.2% | 0.0% | 0.0% | 0.1% |
| South Africa | 685 | 2.2% | 4.3% | 5.8% | 3.8% | 0.0% | 0.0% | 0.0% | 79.9% | 2.0% | 0.1% | 2.0% | 0.0% |
| India (North) | 693 | 9.3% | 2.2% | 0.1% | 10.6% | 0.0% | 0.0% | 1.8% | 75.2% | 0.0% | 0.9% | 0.0% | 0.0% |
| Source: api.electricitymap.org | Methodology: https://www.electricitymaps.com/methodology. Some emissions factors are based on IPCC 2014 defaults, while some are based on more accurate regional factors. All zones are publicly available on the Carbon intensity and emission factors tab via Google docs link | |||||||||||||
import warnings
from great_tables import GT, md, html, system_fonts
import pandas as pd
warnings.filterwarnings("ignore")
power_cie_prepared_tbl = pd.read_csv("../data/great-tables/examples/power_cie_prepared_tbl.csv")
# Create a Great Tables object
ciep_gt_tbl = GT(data=power_cie_prepared_tbl)
# Apply wider color ranges & formatting
gt_tbl = (
ciep_gt_tbl.fmt_percent(
columns=[
"Hydro",
"Nuclear",
"Wind",
"Solar",
"Geothermal",
"Biomass",
"Gas",
"Coal",
"Oil",
"Unknown",
"Hydro Discharge",
"Battery Discharge",
],
decimals=1,
)
.fmt_number(columns=["CO2 Intensity"], decimals=0)
.data_color(
columns=["CO2 Intensity"],
palette=[
"#00A600",
"#E6E600",
"#E8C32E",
"#D69C4E",
"#Dc863B",
"sienna",
"sienna4",
"tomato4",
"brown",
],
domain=[0, 900],
)
.data_color(
columns=["Hydro", "Nuclear", "Wind", "Solar", "Geothermal"],
palette=["#00A600", "chartreuse3", "chartreuse4", "snow"][::-1],
domain=[0, 1],
)
.data_color(
columns=["Hydro", "Geothermal"],
palette=["#00A600", "chartreuse3", "chartreuse4", "snow"][::-1],
domain=[0, 1],
)
.data_color(
columns=["Biomass"], palette=["snow", "#EEC900", "#E8C32E", "#D69C4E"], domain=[0, 0.3]
)
.data_color(
columns=["Gas", "Coal", "Oil"],
palette=["tomato4", "sienna4", "#D69C4E", "#Dc863B", "snow"][::-1],
domain=[0, 1],
)
.data_color(
columns=["Zone", "Unknown", "Hydro Discharge", "Battery Discharge"],
palette=["snow", "snow", "snow", "snow"],
)
.cols_width(
{
"CO2 Intensity": "58px",
"Hydro": "58px",
"Nuclear": "58px",
"Wind": "58px",
"Solar": "58px",
"Geothermal": "58px",
"Biomass": "58px",
"Gas": "58px",
"Coal": "58px",
"Oil": "58px",
"Unknown": "58px",
"Hydro Discharge": "58px",
"Battery Discharge": "58px",
}
)
.tab_header(
title=md(
"2023 Mean **Carbon Intensity** (gCO2eq/kWh) and **Power Consumption** Breakdown (%)"
)
)
.tab_source_note(
md(
'<br><div style="text-align: left;">'
"**Source**: api.electricitymap.org"
" | **Methodology**: https://www.electricitymaps.com/methodology."
" Some emissions factors are based on IPCC 2014 defaults, while some are based on more accurate regional factors."
" <br>All zones are publicly available on the *Carbon intensity and emission factors* tab via Google docs link<br>"
"</div>"
"<br>"
)
)
.tab_options(
source_notes_font_size="x-small",
source_notes_padding=3,
table_font_names=system_fonts("humanist"),
data_row_padding="1px",
heading_background_color="antiquewhite",
source_notes_background_color="antiquewhite",
column_labels_background_color="antiquewhite",
table_background_color="snow",
data_row_padding_horizontal=3,
column_labels_padding_horizontal=58,
)
.cols_align(align="center")
.cols_align(align="left", columns=["Zone"])
.opt_table_outline()
)
gt_tbl| 2023 Mean Carbon Intensity (gCO2eq/kWh) and Power Consumption Breakdown (%) | |||||||||||||
| Zone | CO2 Intensity | Hydro | Nuclear | Wind | Solar | Geothermal | Biomass | Gas | Coal | Oil | Unknown | Hydro Discharge | Battery Discharge |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Sweden | 26 | 39.1% | 26.8% | 27.7% | 0.1% | 0.0% | 0.4% | 0.4% | 0.8% | 0.0% | 4.6% | 0.1% | 0.0% |
| Iceland | 28 | 69.4% | 0.0% | 0.0% | 0.0% | 30.6% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% |
| Quebec | 35 | 90.1% | 2.1% | 4.4% | 0.0% | 0.0% | 1.9% | 1.4% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% |
| France | 46 | 12.3% | 65.4% | 10.3% | 1.8% | 0.0% | 1.0% | 7.1% | 0.3% | 0.3% | 0.1% | 1.4% | 0.0% |
| Ontario | 104 | 23.3% | 49.4% | 8.7% | 0.1% | 0.0% | 0.2% | 18.1% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% |
| New Zealand | 106 | 60.5% | 0.0% | 7.7% | 0.1% | 19.0% | 0.0% | 6.8% | 3.7% | 0.0% | 2.2% | 0.0% | 0.0% |
| Finland | 107 | 20.2% | 36.5% | 24.1% | 0.1% | 0.0% | 6.2% | 3.0% | 8.1% | 0.0% | 1.8% | 0.0% | 0.0% |
| South Australia | 132 | 0.7% | 0.0% | 42.6% | 33.7% | 0.0% | 0.0% | 13.3% | 9.0% | 0.0% | 0.0% | 0.0% | 0.7% |
| Spain | 132 | 17.1% | 24.2% | 25.1% | 8.0% | 0.0% | 2.0% | 18.8% | 1.3% | 0.2% | 0.3% | 3.0% | 0.0% |
| Belgium | 147 | 1.3% | 39.6% | 25.2% | 3.6% | 0.0% | 2.8% | 19.4% | 1.7% | 0.1% | 4.9% | 1.2% | 0.0% |
| Tasmania | 162 | 49.0% | 0.0% | 22.6% | 10.8% | 0.0% | 0.0% | 1.5% | 16.1% | 0.0% | 0.0% | 0.0% | 0.0% |
| East Denmark | 184 | 6.4% | 5.5% | 48.4% | 1.3% | 0.0% | 16.8% | 7.7% | 10.8% | 1.4% | 1.4% | 0.4% | 0.0% |
| West Denmark | 188 | 8.8% | 2.2% | 56.3% | 1.6% | 0.0% | 7.6% | 8.5% | 13.0% | 0.9% | 0.4% | 0.6% | 0.0% |
| Great Britain | 214 | 3.8% | 12.4% | 35.9% | 2.7% | 0.0% | 6.2% | 35.1% | 2.0% | 0.0% | 1.0% | 1.0% | 0.0% |
| Netherlands | 218 | 1.1% | 3.9% | 46.7% | 10.8% | 0.0% | 4.6% | 22.4% | 8.6% | 0.8% | 1.1% | 0.2% | 0.0% |
| New York ISO | 275 | 23.7% | 22.8% | 4.9% | 0.0% | 0.0% | 0.1% | 46.9% | 0.0% | 0.0% | 1.6% | 0.0% | 0.0% |
| Italy (North) | 307 | 22.7% | 14.5% | 3.9% | 2.9% | 0.2% | 3.1% | 38.4% | 1.5% | 0.2% | 9.3% | 3.3% | 0.0% |
| California | 328 | 8.4% | 12.7% | 7.9% | 12.0% | 3.0% | 1.8% | 48.5% | 2.1% | 0.0% | 1.2% | 0.0% | 2.6% |
| Germany | 389 | 4.4% | 2.8% | 39.7% | 3.3% | 0.0% | 8.7% | 14.4% | 23.3% | 0.6% | 0.6% | 2.1% | 0.0% |
| Ireland | 389 | 3.7% | 0.8% | 38.5% | 0.2% | 0.0% | 2.5% | 42.4% | 9.7% | 2.0% | 0.1% | 0.1% | 0.0% |
| Western Australia | 417 | 0.0% | 0.0% | 14.1% | 33.8% | 0.0% | 0.3% | 24.2% | 27.1% | 0.3% | 0.0% | 0.0% | 0.3% |
| Texas | 432 | 0.0% | 9.1% | 22.3% | 6.0% | 0.0% | 0.0% | 46.1% | 16.1% | 0.0% | 0.4% | 0.0% | 0.0% |
| Alberta | 447 | 1.9% | 0.0% | 12.4% | 1.1% | 0.0% | 2.5% | 70.7% | 7.2% | 0.0% | 4.1% | 0.0% | 0.0% |
| Victoria | 508 | 3.9% | 0.0% | 17.5% | 19.0% | 0.0% | 0.0% | 0.3% | 59.1% | 0.0% | 0.0% | 0.0% | 0.2% |
| New South Wales | 578 | 3.2% | 0.0% | 9.5% | 23.7% | 0.0% | 0.2% | 0.7% | 62.6% | 0.0% | 0.0% | 0.0% | 0.1% |
| Queensland | 662 | 1.9% | 0.0% | 3.8% | 21.1% | 0.0% | 0.0% | 7.2% | 65.7% | 0.2% | 0.0% | 0.0% | 0.1% |
| South Africa | 685 | 2.2% | 4.3% | 5.8% | 3.8% | 0.0% | 0.0% | 0.0% | 79.9% | 2.0% | 0.1% | 2.0% | 0.0% |
| India (North) | 693 | 9.3% | 2.2% | 0.1% | 10.6% | 0.0% | 0.0% | 1.8% | 75.2% | 0.0% | 0.9% | 0.0% | 0.0% |
Source: api.electricitymap.org | Methodology: https://www.electricitymaps.com/methodology. Some emissions factors are based on IPCC 2014 defaults, while some are based on more accurate regional factors. All zones are publicly available on the Carbon intensity and emission factors tab via Google docs link | |||||||||||||
power_cie_prepared_tbl <- read.csv(
"../data/great-tables/examples/power_cie_prepared_tbl.csv",
check.names = FALSE
)
shares <- c(
"Hydro", "Nuclear", "Wind", "Solar", "Geothermal", "Biomass", "Gas", "Coal",
"Oil", "Unknown", "Hydro Discharge", "Battery Discharge"
)
power_cie_prepared_tbl |>
gt() |>
fmt_percent(columns = all_of(shares), decimals = 1) |>
fmt_number(columns = "CO2 Intensity", decimals = 0) |>
data_color(
columns = "CO2 Intensity",
palette = c(
"#00A600", "#E6E600", "#E8C32E", "#D69C4E", "#Dc863B",
"sienna", "sienna4", "tomato4", "brown"
),
domain = c(0, 900)
) |>
data_color(
columns = c(Hydro, Nuclear, Wind, Solar, Geothermal),
palette = rev(c("#00A600", "chartreuse3", "chartreuse4", "snow")),
domain = c(0, 1)
) |>
data_color(
columns = Biomass,
palette = c("snow", "#EEC900", "#E8C32E", "#D69C4E"),
domain = c(0, 0.3)
) |>
data_color(
columns = c(Gas, Coal, Oil),
palette = rev(c("tomato4", "sienna4", "#D69C4E", "#Dc863B", "snow")),
domain = c(0, 1)
) |>
data_color(
columns = c(Zone, Unknown, `Hydro Discharge`, `Battery Discharge`),
palette = c("snow", "snow", "snow", "snow")
) |>
cols_width(-Zone ~ px(58)) |>
tab_header(
title = md("2023 Mean **Carbon Intensity** (gCO2eq/kWh) and **Power Consumption** Breakdown (%)")
) |>
tab_source_note(
md(paste0(
'<br><div style="text-align: left;">',
"**Source**: api.electricitymap.org",
" | **Methodology**: https://www.electricitymaps.com/methodology.",
" Some emissions factors are based on IPCC 2014 defaults, while some are based on more accurate regional factors.",
" <br>All zones are publicly available on the *Carbon intensity and emission factors* tab via Google docs link<br>",
"</div>",
"<br>"
))
) |>
opt_table_font(stack = "humanist") |>
tab_options(
source_notes.font.size = "x-small",
source_notes.padding = px(3),
data_row.padding = px(1),
heading.background.color = "antiquewhite",
source_notes.background.color = "antiquewhite",
column_labels.background.color = "antiquewhite",
table.background.color = "snow",
data_row.padding.horizontal = px(3),
column_labels.padding.horizontal = px(58)
) |>
cols_align(align = "center") |>
cols_align(align = "left", columns = Zone) |>
opt_table_outline()| 2023 Mean Carbon Intensity (gCO2eq/kWh) and Power Consumption Breakdown (%) | |||||||||||||
| Zone | CO2 Intensity | Hydro | Nuclear | Wind | Solar | Geothermal | Biomass | Gas | Coal | Oil | Unknown | Hydro Discharge | Battery Discharge |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Sweden | 26 | 39.1% | 26.8% | 27.7% | 0.1% | 0.0% | 0.4% | 0.4% | 0.8% | 0.0% | 4.6% | 0.1% | 0.0% |
| Iceland | 28 | 69.4% | 0.0% | 0.0% | 0.0% | 30.6% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% |
| Quebec | 35 | 90.1% | 2.1% | 4.4% | 0.0% | 0.0% | 1.9% | 1.4% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% |
| France | 46 | 12.3% | 65.4% | 10.3% | 1.8% | 0.0% | 1.0% | 7.1% | 0.3% | 0.3% | 0.1% | 1.4% | 0.0% |
| Ontario | 104 | 23.3% | 49.4% | 8.7% | 0.1% | 0.0% | 0.2% | 18.1% | 0.0% | 0.0% | 0.0% | 0.0% | 0.0% |
| New Zealand | 106 | 60.5% | 0.0% | 7.7% | 0.1% | 19.0% | 0.0% | 6.8% | 3.7% | 0.0% | 2.2% | 0.0% | 0.0% |
| Finland | 107 | 20.2% | 36.5% | 24.1% | 0.1% | 0.0% | 6.2% | 3.0% | 8.1% | 0.0% | 1.8% | 0.0% | 0.0% |
| South Australia | 132 | 0.7% | 0.0% | 42.6% | 33.7% | 0.0% | 0.0% | 13.3% | 9.0% | 0.0% | 0.0% | 0.0% | 0.7% |
| Spain | 132 | 17.1% | 24.2% | 25.1% | 8.0% | 0.0% | 2.0% | 18.8% | 1.3% | 0.2% | 0.3% | 3.0% | 0.0% |
| Belgium | 147 | 1.3% | 39.6% | 25.2% | 3.6% | 0.0% | 2.8% | 19.4% | 1.7% | 0.1% | 4.9% | 1.2% | 0.0% |
| Tasmania | 162 | 49.0% | 0.0% | 22.6% | 10.8% | 0.0% | 0.0% | 1.5% | 16.1% | 0.0% | 0.0% | 0.0% | 0.0% |
| East Denmark | 184 | 6.4% | 5.5% | 48.4% | 1.3% | 0.0% | 16.8% | 7.7% | 10.8% | 1.4% | 1.4% | 0.4% | 0.0% |
| West Denmark | 188 | 8.8% | 2.2% | 56.3% | 1.6% | 0.0% | 7.6% | 8.5% | 13.0% | 0.9% | 0.4% | 0.6% | 0.0% |
| Great Britain | 214 | 3.8% | 12.4% | 35.9% | 2.7% | 0.0% | 6.2% | 35.1% | 2.0% | 0.0% | 1.0% | 1.0% | 0.0% |
| Netherlands | 218 | 1.1% | 3.9% | 46.7% | 10.8% | 0.0% | 4.6% | 22.4% | 8.6% | 0.8% | 1.1% | 0.2% | 0.0% |
| New York ISO | 275 | 23.7% | 22.8% | 4.9% | 0.0% | 0.0% | 0.1% | 46.9% | 0.0% | 0.0% | 1.6% | 0.0% | 0.0% |
| Italy (North) | 307 | 22.7% | 14.5% | 3.9% | 2.9% | 0.2% | 3.1% | 38.4% | 1.5% | 0.2% | 9.3% | 3.3% | 0.0% |
| California | 328 | 8.4% | 12.7% | 7.9% | 12.0% | 3.0% | 1.8% | 48.5% | 2.1% | 0.0% | 1.2% | 0.0% | 2.6% |
| Germany | 389 | 4.4% | 2.8% | 39.7% | 3.3% | 0.0% | 8.7% | 14.4% | 23.3% | 0.6% | 0.6% | 2.1% | 0.0% |
| Ireland | 389 | 3.7% | 0.8% | 38.5% | 0.2% | 0.0% | 2.5% | 42.4% | 9.7% | 2.0% | 0.1% | 0.1% | 0.0% |
| Western Australia | 417 | 0.0% | 0.0% | 14.1% | 33.8% | 0.0% | 0.3% | 24.2% | 27.1% | 0.3% | 0.0% | 0.0% | 0.3% |
| Texas | 432 | 0.0% | 9.1% | 22.3% | 6.0% | 0.0% | 0.0% | 46.1% | 16.1% | 0.0% | 0.4% | 0.0% | 0.0% |
| Alberta | 447 | 1.9% | 0.0% | 12.4% | 1.1% | 0.0% | 2.5% | 70.7% | 7.2% | 0.0% | 4.1% | 0.0% | 0.0% |
| Victoria | 508 | 3.9% | 0.0% | 17.5% | 19.0% | 0.0% | 0.0% | 0.3% | 59.1% | 0.0% | 0.0% | 0.0% | 0.2% |
| New South Wales | 578 | 3.2% | 0.0% | 9.5% | 23.7% | 0.0% | 0.2% | 0.7% | 62.6% | 0.0% | 0.0% | 0.0% | 0.1% |
| Queensland | 662 | 1.9% | 0.0% | 3.8% | 21.1% | 0.0% | 0.0% | 7.2% | 65.7% | 0.2% | 0.0% | 0.0% | 0.1% |
| South Africa | 685 | 2.2% | 4.3% | 5.8% | 3.8% | 0.0% | 0.0% | 0.0% | 79.9% | 2.0% | 0.1% | 2.0% | 0.0% |
| India (North) | 693 | 9.3% | 2.2% | 0.1% | 10.6% | 0.0% | 0.0% | 1.8% | 75.2% | 0.0% | 0.9% | 0.0% | 0.0% |
Source: api.electricitymap.org | Methodology: https://www.electricitymaps.com/methodology. Some emissions factors are based on IPCC 2014 defaults, while some are based on more accurate regional factors. All zones are publicly available on the Carbon intensity and emission factors tab via Google docs link |
|||||||||||||
The last example is a year of sales of coffee equipment, by Michael Chow (source, notebook).
let coffeeSales = readNdjson(newStringStream(coffeeSalesNdjson))
let selRev = startsWith("revenue")
let selProf = startsWith("profit")
let coffeeTable = tabella(coffeeSales):
tabHeader "Sales of Coffee Equipment"
tabSpanner "Revenue", selRev
tabSpanner "Profit", selProf
colsLabel:
revenue_dollars: "Amount"
profit_dollars: "Amount"
revenue_pct: "Percent"
profit_pct: "Percent"
monthly_sales: "Monthly Sales"
icon: ""
product: "Product"
# formatting ----
fmtNumber endsWith("dollars"), compact = true, pattern = "${x}", nSigfig = 3
fmtPercent endsWith("pct"), decimals = 0
# style ----
tabStyle style.fill("aliceblue"), loc.body(cols = selRev)
tabStyle style.fill("papayawhip"), loc.body(cols = selProf)
tabStyle style.text(weight = "bold"), loc.body(rows = where(it["product"] == "Total"))
fmtNanoplot "monthly_sales", plotType = "bar"
fmtImage "icon", path = coffeeIcons
subMissing(missingText = "")
nbTable coffeeTable
| Sales of Coffee Equipment | ||||||
|---|---|---|---|---|---|---|
| Product | Revenue | Profit | Monthly Sales | |||
| Amount | Percent | Amount | Percent | |||
| Grinder | $905K | 3% | $568K | 4% | ||
| Moka pot | $2.05M | 7% | $181K | 1% | ||
| Cold brew | $289K | 1% | $242K | 2% | ||
| Filter | $404K | 1% | $70.0K | 0% | ||
| Drip machine | $2.63M | 9% | $1.37M | 9% | ||
| AeroPress | $2.60M | 9% | $1.29M | 9% | ||
| Pour over | $846K | 3% | $365K | 2% | ||
| French press | $1.11M | 4% | $748K | 5% | ||
| Cezve | $2.51M | 9% | $1.97M | 13% | ||
| Chemex | $3.14M | 11% | $818K | 6% | ||
| Scale | $3.80M | 13% | $2.91M | 20% | ||
| Kettle | $756K | 3% | $618K | 4% | ||
| Espresso Machine | $8.41M | 29% | $3.64M | 25% | ||
| Total | $29.4M | 100% | $14.8M | 100% | ||
import polars as pl
import polars.selectors as cs
from great_tables import GT, loc, style
coffee_sales = pl.read_ndjson("../data/great-tables/examples/coffee-sales.ndjson")
sel_rev = cs.starts_with("revenue")
sel_prof = cs.starts_with("profit")
# yo
coffee_table = (
GT(coffee_sales)
.tab_header("Sales of Coffee Equipment")
.tab_spanner(label="Revenue", columns=sel_rev)
.tab_spanner(label="Profit", columns=sel_prof)
.cols_label(
revenue_dollars="Amount",
profit_dollars="Amount",
revenue_pct="Percent",
profit_pct="Percent",
monthly_sales="Monthly Sales",
icon="",
product="Product",
)
# formatting ----
.fmt_number(
columns=cs.ends_with("dollars"),
compact=True,
pattern="${x}",
n_sigfig=3,
)
.fmt_percent(columns=cs.ends_with("pct"), decimals=0)
# style ----
.tab_style(
style=style.fill(color="aliceblue"),
locations=loc.body(columns=sel_rev),
)
.tab_style(
style=style.fill(color="papayawhip"),
locations=loc.body(columns=sel_prof),
)
.tab_style(
style=style.text(weight="bold"),
locations=loc.body(rows=pl.col("product") == "Total"),
)
.fmt_nanoplot("monthly_sales", plot_type="bar")
.fmt_image("icon", path="../data/great-tables/examples/coffee-table-icons/")
.sub_missing(missing_text="")
)
coffee_table| Sales of Coffee Equipment | ||||||
| Product | Revenue | Profit | Monthly Sales | |||
|---|---|---|---|---|---|---|
| Amount | Percent | Amount | Percent | |||
| Grinder | $904K | 3% | $568K | 4% | ||
| Moka pot | $2.05M | 7% | $181K | 1% | ||
| Cold brew | $289K | 1% | $242K | 2% | ||
| Filter | $404K | 1% | $70.0K | 0% | ||
| Drip machine | $2.63M | 9% | $1.37M | 9% | ||
| AeroPress | $2.60M | 9% | $1.29M | 9% | ||
| Pour over | $846K | 3% | $365K | 2% | ||
| French press | $1.11M | 4% | $748K | 5% | ||
| Cezve | $2.51M | 9% | $1.97M | 13% | ||
| Chemex | $3.14M | 11% | $818K | 6% | ||
| Scale | $3.80M | 13% | $2.91M | 20% | ||
| Kettle | $756K | 3% | $618K | 4% | ||
| Espresso Machine | $8.41M | 29% | $3.64M | 25% | ||
| Total | $29.4M | 100% | $14.8M | 100% | ||
coffee_sales <-
jsonlite::stream_in(
file("../data/great-tables/examples/coffee-sales.ndjson"),
verbose = FALSE
) |>
as_tibble() |>
unnest_wider(monthly_sales, names_sep = "_")
coffee_sales |>
gt() |>
tab_header("Sales of Coffee Equipment") |>
tab_spanner(label = "Revenue", columns = starts_with("revenue")) |>
tab_spanner(label = "Profit", columns = starts_with("profit")) |>
cols_label(
revenue_dollars = "Amount",
profit_dollars = "Amount",
revenue_pct = "Percent",
profit_pct = "Percent",
icon = "",
product = "Product"
) |>
# formatting ----
fmt_number(
columns = ends_with("dollars"),
suffixing = TRUE,
pattern = "${x}",
n_sigfig = 3
) |>
fmt_percent(columns = ends_with("pct"), decimals = 0) |>
# style ----
tab_style(
style = cell_fill(color = "aliceblue"),
locations = cells_body(columns = starts_with("revenue"))
) |>
tab_style(
style = cell_fill(color = "papayawhip"),
locations = cells_body(columns = starts_with("profit"))
) |>
tab_style(
style = cell_text(weight = "bold"),
locations = cells_body(rows = product == "Total")
) |>
cols_nanoplot(
columns = starts_with("monthly_sales_"),
plot_type = "bar",
new_col_name = "monthly_sales",
new_col_label = "Monthly Sales"
) |>
fmt_image(columns = icon, path = "../data/great-tables/examples/coffee-table-icons/") |>
sub_missing(missing_text = "")| Sales of Coffee Equipment | ||||||
| Product |
Revenue
|
Profit
|
Monthly Sales | |||
|---|---|---|---|---|---|---|
| Amount | Percent | Amount | Percent | |||
| Grinder | $904K | 3% | $568K | 4% | ||
| Moka pot | $2.05M | 7% | $181K | 1% | ||
| Cold brew | $289K | 1% | $242K | 2% | ||
| Filter | $404K | 1% | $70.0K | 0% | ||
| Drip machine | $2.63M | 9% | $1.37M | 9% | ||
| AeroPress | $2.60M | 9% | $1.29M | 9% | ||
| Pour over | $846K | 3% | $365K | 2% | ||
| French press | $1.11M | 4% | $748K | 5% | ||
| Cezve | $2.51M | 9% | $1.97M | 13% | ||
| Chemex | $3.14M | 11% | $818K | 6% | ||
| Scale | $3.80M | 13% | $2.91M | 20% | ||
| Kettle | $756K | 3% | $618K | 4% | ||
| Espresso Machine | $8.41M | 29% | $3.64M | 25% | ||
| Total | $29.4M | 100% | $14.8M | 100% | ||
import tabbook
bookInit()
nbText: "# Examples"
attribution gtExample("Examples", "https://posit-dev.github.io/great-tables/examples/",
"The examples are shown one after the other, with their code, where the original sets them " &
"in a grid with the code folded; the data files are read from the book's folder; the Sports " &
"earnings example has a page of its own, next, and the gt-extras example comes with the other " &
"gt-extras pages.")
nbText: """
These are the tables of the Great Tables Examples page, from a small table of the largest
landmasses to a year of sales of coffee equipment with icons and bar plots.
"""
tabs """
import polars as pl
from great_tables import GT, md, html
from great_tables.data import islands
islands_mini = (
pl.from_pandas(islands).sort("size", descending=True)
.head(10)
)
(
GT(islands_mini)
.tab_header(
title="Large Landmasses of the World",
subtitle="The top ten largest are presented"
)
.tab_stub(rowname_col="name")
.tab_source_note(source_note="Source: The World Almanac and Book of Facts, 1975, page 406.")
.tab_source_note(
source_note=md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
)
.tab_stubhead(label="landmass")
.fmt_integer(columns="size")
)
""", """
library(gt)
library(dplyr)
library(tidyr)
islands_mini <-
tibble(name = names(islands), size = islands) |>
arrange(desc(size)) |>
slice_head(n = 10)
islands_mini |>
gt(rowname_col = "name") |>
tab_header(
title = "Large Landmasses of the World",
subtitle = "The top ten largest are presented"
) |>
tab_source_note(source_note = "Source: The World Almanac and Book of Facts, 1975, page 406.") |>
tab_source_note(
source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
) |>
tab_stubhead(label = "landmass") |>
fmt_integer(columns = size)
""":
nbCode:
import std/streams
import nanuq
import tabelline, tabelline/nanuq, gtdata
let islandsMini = readCsv(newStringStream(islandsCsv))
.sort("size", descending = true)
.head(10)
let islandsTbl = tabella(islandsMini):
tabHeader "Large Landmasses of the World", "The top ten largest are presented"
tabStub "name"
tabSourceNote "Source: The World Almanac and Book of Facts, 1975, page 406."
tabSourceNote md"Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley."
tabStubhead "landmass"
fmtInteger "size"
nbTable islandsTbl
tabelline """
The data steps are written with [nanuq](https://github.com/pietroppeter/nanuq), Polars for Nim,
as the examples write them with Polars; `import tabelline/nanuq` lets `tabella` take its data
frames. `tabStub` chooses the stub, as Great Tables' `tab_stub`; gt names it when the table is
made, which tabelline does too, with `tabella(data, rownameCol = "name")`.
"""
tabs """
from great_tables import GT, html
from great_tables.data import airquality
airquality_mini = airquality.head(10).assign(Year = 1973)
(
GT(airquality_mini)
.tab_header(
title="New York Air Quality Measurements",
subtitle="Daily measurements in New York City (May 1-10, 1973)"
)
.tab_spanner(
label="Time",
columns=["Year", "Month", "Day"]
)
.tab_spanner(
label="Measurement",
columns=["Ozone", "Solar_R", "Wind", "Temp"]
)
.cols_move_to_start(columns=["Year", "Month", "Day"])
.cols_label(
Ozone = html("Ozone,<br>ppbV"),
Solar_R = html("Solar R.,<br>cal/m<sup>2</sup>"),
Wind = html("Wind,<br>mph"),
Temp = html("Temp,<br>°F")
)
)
""", """
airquality_mini <- airquality |> head(10) |> mutate(Year = 1973L)
airquality_mini |>
gt() |>
tab_header(
title = "New York Air Quality Measurements",
subtitle = "Daily measurements in New York City (May 1-10, 1973)"
) |>
tab_spanner(
label = "Time",
columns = c(Year, Month, Day)
) |>
tab_spanner(
label = "Measurement",
columns = c(Ozone, Solar.R, Wind, Temp)
) |>
cols_move_to_start(columns = c(Year, Month, Day)) |>
cols_label(
Ozone = html("Ozone,<br>ppbV"),
Solar.R = html("Solar R.,<br>cal/m<sup>2</sup>"),
Wind = html("Wind,<br>mph"),
Temp = html("Temp,<br>°F")
)
""":
nbCode:
let airqualityMini = readCsv(newStringStream(airqualityCsv), inferSchemaLength = 10_000)
.head(10)
.withColumns(lit(1973).alias("Year"))
let airqualityTbl = tabella(airqualityMini):
tabHeader "New York Air Quality Measurements",
"Daily measurements in New York City (May 1-10, 1973)"
tabSpanner "Time", ["Year", "Month", "Day"]
tabSpanner "Measurement", ["Ozone", "Solar_R", "Wind", "Temp"]
colsMoveToStart ["Year", "Month", "Day"]
colsLabel:
Ozone: html"Ozone,<br>ppbV"
Solar_R: html"Solar R.,<br>cal/m<sup>2</sup>"
Wind: html"Wind,<br>mph"
Temp: html"Temp,<br>°F"
nbTable airqualityTbl
tabs """
from great_tables import GT
from great_tables.data import countrypops
import polars as pl
import polars.selectors as cs
# Get vectors of 2-letter country codes for each region of Oceania
oceania = {
"Australasia": ["AU", "NZ"],
"Melanesia": ["NC", "PG", "SB", "VU"],
"Micronesia": ["FM", "GU", "KI", "MH", "MP", "NR", "PW"],
"Polynesia": ["PF", "WS", "TO", "TV"],
}
# Create a dictionary mapping country to region (e.g. AU -> Australasia)
country_to_region = {
country: region for region, countries in oceania.items() for country in countries
}
wide_pops = (
pl.from_pandas(countrypops)
.filter(
pl.col("country_code_2").is_in(list(country_to_region))
& pl.col("year").is_in([2000, 2010, 2020])
)
.with_columns(pl.col("country_code_2").replace(country_to_region).alias("region"))
.pivot(index=["country_name", "region"], on="year", values="population")
.sort("2020", descending=True)
)
(
GT(wide_pops)
.tab_header(title="Populations of Oceania's Countries in 2000, 2010, and 2020")
.tab_spanner(label="Total Population", columns=cs.all())
.tab_stub(rowname_col="country_name", groupname_col="region")
.fmt_integer()
)
""", """
# Get vectors of 2-letter country codes for each region of Oceania
oceania <- list(
Australasia = c("AU", "NZ"),
Melanesia = c("NC", "PG", "SB", "VU"),
Micronesia = c("FM", "GU", "KI", "MH", "MP", "NR", "PW"),
Polynesia = c("PF", "WS", "TO", "TV")
)
# A named vector mapping country to region (e.g. AU -> Australasia)
country_to_region <- setNames(rep(names(oceania), lengths(oceania)), unlist(oceania))
wide_pops <-
countrypops |>
filter(country_code_2 %in% names(country_to_region), year %in% c(2000, 2010, 2020)) |>
mutate(region = country_to_region[country_code_2]) |>
select(country_name, region, year, population) |>
pivot_wider(names_from = year, values_from = population) |>
arrange(desc(`2020`))
wide_pops |>
gt(rowname_col = "country_name", groupname_col = "region") |>
tab_header(title = "Populations of Oceania's Countries in 2000, 2010, and 2020") |>
tab_spanner(label = "Total Population", columns = everything()) |>
fmt_integer()
""":
nbCode:
import std/[sequtils, tables]
# Get lists of 2-letter country codes for each region of Oceania
let oceania = {
"Australasia": @["AU", "NZ"],
"Melanesia": @["NC", "PG", "SB", "VU"],
"Micronesia": @["FM", "GU", "KI", "MH", "MP", "NR", "PW"],
"Polynesia": @["PF", "WS", "TO", "TV"]}.toOrderedTable
# Create a table mapping country to region (e.g. AU -> Australasia)
var countryToRegion = initOrderedTable[string, string]()
for region, countries in oceania:
for country in countries: countryToRegion[country] = region
var codes: seq[string]
for country in countryToRegion.keys: codes.add country
let widePops = readCsv(newStringStream(countrypopsCsv), inferSchemaLength = 10_000)
.filter(col("country_code_2").isIn(codes) and col("year").isIn([2000, 2010, 2020]))
.withColumns(col("country_code_2").replace(countryToRegion.pairs.toSeq).alias("region"))
.pivot(index = ["country_name", "region"], on = "year", values = "population")
.sort("2020", descending = true)
let widePopsTbl = tabella(widePops, rownameCol = "country_name", groupnameCol = "region"):
tabHeader "Populations of Oceania's Countries in 2000, 2010, and 2020"
tabSpanner "Total Population", everything()
fmtInteger()
nbTable widePopsTbl
tabs """
from great_tables import GT, html
from great_tables.data import towny
towny_mini = (
towny[["name", "website", "density_2021", "land_area_km2", "latitude", "longitude"]]
.sort_values("density_2021", ascending=False)
.head(10)
)
towny_mini["url_name"] = "[" + towny_mini["name"] + "](" + towny_mini["website"] + ")"
towny_mini["location"] = (
"[map](http://maps.google.com/?ie=UTF8&hq=&ll="
+ towny_mini["latitude"].astype(str)
+ ","
+ towny_mini["longitude"].astype(str)
+ "&z=13)"
)
(
GT(
towny_mini[["url_name", "location", "land_area_km2", "density_2021"]],
rowname_col="url_name",
)
.tab_header(
title="The Municipalities of Ontario",
subtitle="The top 10 highest population density in 2021",
)
.tab_stubhead(label="Municipality")
.fmt_markdown(columns=["url_name", "location"])
.fmt_number(columns=["land_area_km2", "density_2021"], decimals=1)
.cols_label(
land_area_km2=html("land area, <br>km<sup>2</sup>"),
density_2021=html("density, <br>people/km<sup>2</sup>"),
)
)
""", """
towny_mini <-
towny |>
select(name, website, density_2021, land_area_km2, latitude, longitude) |>
arrange(desc(density_2021)) |>
slice_head(n = 10) |>
mutate(
url_name = paste0("[", name, "](", website, ")"),
location = paste0(
"[map](http://maps.google.com/?ie=UTF8&hq=&ll=",
latitude, ",", longitude, "&z=13)"
)
)
towny_mini |>
select(url_name, location, land_area_km2, density_2021) |>
gt(rowname_col = "url_name") |>
tab_header(
title = "The Municipalities of Ontario",
subtitle = "The top 10 highest population density in 2021"
) |>
tab_stubhead(label = "Municipality") |>
fmt_markdown(columns = c(url_name, location)) |>
fmt_number(columns = c(land_area_km2, density_2021), decimals = 1) |>
cols_label(
land_area_km2 = html("land area, <br>km<sup>2</sup>"),
density_2021 = html("density, <br>people/km<sup>2</sup>")
)
""":
nbCode:
let townyMini = readCsv(newStringStream(townyCsv), inferSchemaLength = 10_000)
.select("name", "website", "density_2021", "land_area_km2", "latitude", "longitude")
.sort("density_2021", descending = true)
.head(10)
.withColumns(
concatStr([lit("["), col("name"), lit("]("), col("website"), lit(")")]).alias("url_name"),
concatStr([lit("[map](http://maps.google.com/?ie=UTF8&hq=&ll="),
col("latitude").`cast`(String), lit(","), col("longitude").`cast`(String),
lit("&z=13)")]).alias("location"))
.select("url_name", "location", "land_area_km2", "density_2021")
let townyTbl = tabella(townyMini, rownameCol = "url_name"):
tabHeader "The Municipalities of Ontario", "The top 10 highest population density in 2021"
tabStubhead "Municipality"
fmtMarkdown ["url_name", "location"]
fmtNumber ["land_area_km2", "density_2021"], decimals = 1
colsLabel:
land_area_km2: html"land area, <br>km<sup>2</sup>"
density_2021: html"density, <br>people/km<sup>2</sup>"
nbTable townyTbl
tabs """
from great_tables import GT, html
from great_tables.data import sza
import polars as pl
import polars.selectors as cs
sza_pivot = (
pl.from_pandas(sza)
.filter((pl.col("latitude") == "20") & (pl.col("tst") <= "1200"))
.select(pl.col("*").exclude("latitude"))
.drop_nulls()
.pivot(values="sza", index="month", on="tst", sort_columns=True)
)
(
GT(sza_pivot, rowname_col="month")
.data_color(
domain=[90, 0],
palette=["rebeccapurple", "white", "orange"],
na_color="white",
)
.tab_header(
title="Solar Zenith Angles from 05:30 to 12:00",
subtitle=html("Average monthly values at latitude of 20°N."),
)
.sub_missing(missing_text="")
)
""", """
sza_pivot <-
sza |>
filter(latitude == 20, tst <= "1200") |>
select(-latitude) |>
drop_na() |>
pivot_wider(names_from = tst, values_from = sza, names_sort = TRUE)
sza_pivot |>
gt(rowname_col = "month") |>
data_color(
domain = c(0, 90),
palette = c("orange", "white", "#663399"), # R has no rebeccapurple
na_color = "white"
) |>
tab_header(
title = "Solar Zenith Angles from 05:30 to 12:00",
subtitle = html("Average monthly values at latitude of 20°N.")
) |>
sub_missing(missing_text = "")
""":
nbCode:
let szaPivot = readCsv(newStringStream(szaCsv), inferSchemaLength = 0) # every column as text
.filter(col("latitude") == "20" and col("tst") <= lit("1200"))
.drop("latitude")
.dropNulls()
.withColumns(col("sza").`cast`(Float64))
.pivot(values = "sza", index = "month", on = "tst", sortColumns = true)
let szaTbl = tabella(szaPivot, rownameCol = "month"):
dataColor domain = (90.0, 0.0), palette = @["rebeccapurple", "white", "orange"],
naColor = "white"
tabHeader "Solar Zenith Angles from 05:30 to 12:00",
html"Average monthly values at latitude of 20°N."
subMissing(missingText = "")
nbTable szaTbl
tabelline """
gt's `data_color()` wants its domain from low to high, so the R tab turns the palette around
instead: 0° (the sun overhead) is orange, 90° (the sun on the horizon) purple, as in Python and
Nim, where the domain may run from high to low. R's colour names are not CSS's, and
`rebeccapurple` is CSS only: the R tab writes it as `#663399`.
"""
nbText: """
The Sports earnings example, the highest paid athletes of 2023, has [a page of its
own](sports_earnings.html).
The next example is the carbon intensity of electricity consumption, by Grant Chalmers
([source](https://gist.github.com/chalg/93ca29e202ab86e7d4caad44726372e4/d17c1b4ba608ae1c3df6ac7fc377065ae5708ba7),
[blog post with the R code](https://chalg.quarto.pub/carbon-intensity-of-electricity-consumption/)).
"""
tabs """
import warnings
from great_tables import GT, md, html, system_fonts
import pandas as pd
warnings.filterwarnings("ignore")
power_cie_prepared_tbl = pd.read_csv("../data/great-tables/examples/power_cie_prepared_tbl.csv")
# Create a Great Tables object
ciep_gt_tbl = GT(data=power_cie_prepared_tbl)
# Apply wider color ranges & formatting
gt_tbl = (
ciep_gt_tbl.fmt_percent(
columns=[
"Hydro",
"Nuclear",
"Wind",
"Solar",
"Geothermal",
"Biomass",
"Gas",
"Coal",
"Oil",
"Unknown",
"Hydro Discharge",
"Battery Discharge",
],
decimals=1,
)
.fmt_number(columns=["CO2 Intensity"], decimals=0)
.data_color(
columns=["CO2 Intensity"],
palette=[
"#00A600",
"#E6E600",
"#E8C32E",
"#D69C4E",
"#Dc863B",
"sienna",
"sienna4",
"tomato4",
"brown",
],
domain=[0, 900],
)
.data_color(
columns=["Hydro", "Nuclear", "Wind", "Solar", "Geothermal"],
palette=["#00A600", "chartreuse3", "chartreuse4", "snow"][::-1],
domain=[0, 1],
)
.data_color(
columns=["Hydro", "Geothermal"],
palette=["#00A600", "chartreuse3", "chartreuse4", "snow"][::-1],
domain=[0, 1],
)
.data_color(
columns=["Biomass"], palette=["snow", "#EEC900", "#E8C32E", "#D69C4E"], domain=[0, 0.3]
)
.data_color(
columns=["Gas", "Coal", "Oil"],
palette=["tomato4", "sienna4", "#D69C4E", "#Dc863B", "snow"][::-1],
domain=[0, 1],
)
.data_color(
columns=["Zone", "Unknown", "Hydro Discharge", "Battery Discharge"],
palette=["snow", "snow", "snow", "snow"],
)
.cols_width(
{
"CO2 Intensity": "58px",
"Hydro": "58px",
"Nuclear": "58px",
"Wind": "58px",
"Solar": "58px",
"Geothermal": "58px",
"Biomass": "58px",
"Gas": "58px",
"Coal": "58px",
"Oil": "58px",
"Unknown": "58px",
"Hydro Discharge": "58px",
"Battery Discharge": "58px",
}
)
.tab_header(
title=md(
"2023 Mean **Carbon Intensity** (gCO2eq/kWh) and **Power Consumption** Breakdown (%)"
)
)
.tab_source_note(
md(
'<br><div style="text-align: left;">'
"**Source**: api.electricitymap.org"
" | **Methodology**: https://www.electricitymaps.com/methodology."
" Some emissions factors are based on IPCC 2014 defaults, while some are based on more accurate regional factors."
" <br>All zones are publicly available on the *Carbon intensity and emission factors* tab via Google docs link<br>"
"</div>"
"<br>"
)
)
.tab_options(
source_notes_font_size="x-small",
source_notes_padding=3,
table_font_names=system_fonts("humanist"),
data_row_padding="1px",
heading_background_color="antiquewhite",
source_notes_background_color="antiquewhite",
column_labels_background_color="antiquewhite",
table_background_color="snow",
data_row_padding_horizontal=3,
column_labels_padding_horizontal=58,
)
.cols_align(align="center")
.cols_align(align="left", columns=["Zone"])
.opt_table_outline()
)
gt_tbl
""", """
power_cie_prepared_tbl <- read.csv(
"../data/great-tables/examples/power_cie_prepared_tbl.csv",
check.names = FALSE
)
shares <- c(
"Hydro", "Nuclear", "Wind", "Solar", "Geothermal", "Biomass", "Gas", "Coal",
"Oil", "Unknown", "Hydro Discharge", "Battery Discharge"
)
power_cie_prepared_tbl |>
gt() |>
fmt_percent(columns = all_of(shares), decimals = 1) |>
fmt_number(columns = "CO2 Intensity", decimals = 0) |>
data_color(
columns = "CO2 Intensity",
palette = c(
"#00A600", "#E6E600", "#E8C32E", "#D69C4E", "#Dc863B",
"sienna", "sienna4", "tomato4", "brown"
),
domain = c(0, 900)
) |>
data_color(
columns = c(Hydro, Nuclear, Wind, Solar, Geothermal),
palette = rev(c("#00A600", "chartreuse3", "chartreuse4", "snow")),
domain = c(0, 1)
) |>
data_color(
columns = Biomass,
palette = c("snow", "#EEC900", "#E8C32E", "#D69C4E"),
domain = c(0, 0.3)
) |>
data_color(
columns = c(Gas, Coal, Oil),
palette = rev(c("tomato4", "sienna4", "#D69C4E", "#Dc863B", "snow")),
domain = c(0, 1)
) |>
data_color(
columns = c(Zone, Unknown, `Hydro Discharge`, `Battery Discharge`),
palette = c("snow", "snow", "snow", "snow")
) |>
cols_width(-Zone ~ px(58)) |>
tab_header(
title = md("2023 Mean **Carbon Intensity** (gCO2eq/kWh) and **Power Consumption** Breakdown (%)")
) |>
tab_source_note(
md(paste0(
'<br><div style="text-align: left;">',
"**Source**: api.electricitymap.org",
" | **Methodology**: https://www.electricitymaps.com/methodology.",
" Some emissions factors are based on IPCC 2014 defaults, while some are based on more accurate regional factors.",
" <br>All zones are publicly available on the *Carbon intensity and emission factors* tab via Google docs link<br>",
"</div>",
"<br>"
))
) |>
opt_table_font(stack = "humanist") |>
tab_options(
source_notes.font.size = "x-small",
source_notes.padding = px(3),
data_row.padding = px(1),
heading.background.color = "antiquewhite",
source_notes.background.color = "antiquewhite",
column_labels.background.color = "antiquewhite",
table.background.color = "snow",
data_row.padding.horizontal = px(3),
column_labels.padding.horizontal = px(58)
) |>
cols_align(align = "center") |>
cols_align(align = "left", columns = Zone) |>
opt_table_outline()
""":
nbCode:
let powerCie = readCsv(newStringStream(powerCieCsv), inferSchemaLength = 10_000)
let widths = powerCie.columns[1 .. ^1].mapIt((it, "58px")) # every column but Zone
let shares = ["Hydro", "Nuclear", "Wind", "Solar", "Geothermal", "Biomass", "Gas", "Coal",
"Oil", "Unknown", "Hydro Discharge", "Battery Discharge"]
let powerTbl = tabella(powerCie):
fmtPercent shares, decimals = 1
fmtNumber "CO2 Intensity", decimals = 0
dataColor "CO2 Intensity", domain = (0.0, 900.0), palette = @["#00A600", "#E6E600",
"#E8C32E", "#D69C4E", "#Dc863B", "sienna", "sienna4", "tomato4", "brown"]
dataColor ["Hydro", "Nuclear", "Wind", "Solar", "Geothermal"], domain = (0.0, 1.0),
palette = @["snow", "chartreuse4", "chartreuse3", "#00A600"]
dataColor "Biomass", domain = (0.0, 0.3), palette = @["snow", "#EEC900", "#E8C32E", "#D69C4E"]
dataColor ["Gas", "Coal", "Oil"], domain = (0.0, 1.0),
palette = @["snow", "#Dc863B", "#D69C4E", "sienna4", "tomato4"]
dataColor ["Zone", "Unknown", "Hydro Discharge", "Battery Discharge"],
palette = @["snow", "snow", "snow", "snow"]
colsWidth widths
tabHeader md"2023 Mean **Carbon Intensity** (gCO2eq/kWh) and **Power Consumption** Breakdown (%)"
tabSourceNote md("**Source**: api.electricitymap.org" &
" | **Methodology**: https://www.electricitymaps.com/methodology." &
" Some emissions factors are based on IPCC 2014 defaults, while some are based on more accurate regional factors." &
"<br>All zones are publicly available on the *Carbon intensity and emission factors* tab via Google docs link")
optTableFont systemFonts("humanist")
tabOptions(sourceNotesFontSize = "x-small", sourceNotesPadding = "3px",
dataRowPadding = "1px", headingBackgroundColor = "antiquewhite",
sourceNotesBackgroundColor = "antiquewhite",
columnLabelsBackgroundColor = "antiquewhite", tableBackgroundColor = "snow",
dataRowPaddingHorizontal = "3px", columnLabelsPaddingHorizontal = "58px")
colsAlign alCenter
colsAlign alLeft, "Zone"
optTableOutline()
nbTable powerTbl
tabelline """
CSS names 148 colours, R 657: tabelline knows them all, as gt and Great Tables do, so R's
numbered shades (`sienna4`, `chartreuse3`) are written as they are; in a table they become
hex values, which CSS reads. The source note is Markdown with its line breaks
and left alignment left out: the notes of a tabelline table are already aligned to the left.
"""
nbText: """
The last example is a year of sales of coffee equipment, by Michael Chow
([source](https://github.com/machow/coffee-sales-data/),
[notebook](https://github.com/machow/coffee-sales-data/blob/main/example_notebook.ipynb)).
"""
tabs """
import polars as pl
import polars.selectors as cs
from great_tables import GT, loc, style
coffee_sales = pl.read_ndjson("../data/great-tables/examples/coffee-sales.ndjson")
sel_rev = cs.starts_with("revenue")
sel_prof = cs.starts_with("profit")
# yo
coffee_table = (
GT(coffee_sales)
.tab_header("Sales of Coffee Equipment")
.tab_spanner(label="Revenue", columns=sel_rev)
.tab_spanner(label="Profit", columns=sel_prof)
.cols_label(
revenue_dollars="Amount",
profit_dollars="Amount",
revenue_pct="Percent",
profit_pct="Percent",
monthly_sales="Monthly Sales",
icon="",
product="Product",
)
# formatting ----
.fmt_number(
columns=cs.ends_with("dollars"),
compact=True,
pattern="${x}",
n_sigfig=3,
)
.fmt_percent(columns=cs.ends_with("pct"), decimals=0)
# style ----
.tab_style(
style=style.fill(color="aliceblue"),
locations=loc.body(columns=sel_rev),
)
.tab_style(
style=style.fill(color="papayawhip"),
locations=loc.body(columns=sel_prof),
)
.tab_style(
style=style.text(weight="bold"),
locations=loc.body(rows=pl.col("product") == "Total"),
)
.fmt_nanoplot("monthly_sales", plot_type="bar")
.fmt_image("icon", path="../data/great-tables/examples/coffee-table-icons/")
.sub_missing(missing_text="")
)
coffee_table
""", """
coffee_sales <-
jsonlite::stream_in(
file("../data/great-tables/examples/coffee-sales.ndjson"),
verbose = FALSE
) |>
as_tibble() |>
unnest_wider(monthly_sales, names_sep = "_")
coffee_sales |>
gt() |>
tab_header("Sales of Coffee Equipment") |>
tab_spanner(label = "Revenue", columns = starts_with("revenue")) |>
tab_spanner(label = "Profit", columns = starts_with("profit")) |>
cols_label(
revenue_dollars = "Amount",
profit_dollars = "Amount",
revenue_pct = "Percent",
profit_pct = "Percent",
icon = "",
product = "Product"
) |>
# formatting ----
fmt_number(
columns = ends_with("dollars"),
suffixing = TRUE,
pattern = "${x}",
n_sigfig = 3
) |>
fmt_percent(columns = ends_with("pct"), decimals = 0) |>
# style ----
tab_style(
style = cell_fill(color = "aliceblue"),
locations = cells_body(columns = starts_with("revenue"))
) |>
tab_style(
style = cell_fill(color = "papayawhip"),
locations = cells_body(columns = starts_with("profit"))
) |>
tab_style(
style = cell_text(weight = "bold"),
locations = cells_body(rows = product == "Total")
) |>
cols_nanoplot(
columns = starts_with("monthly_sales_"),
plot_type = "bar",
new_col_name = "monthly_sales",
new_col_label = "Monthly Sales"
) |>
fmt_image(columns = icon, path = "../data/great-tables/examples/coffee-table-icons/") |>
sub_missing(missing_text = "")
""":
nbCode:
let coffeeSales = readNdjson(newStringStream(coffeeSalesNdjson))
let selRev = startsWith("revenue")
let selProf = startsWith("profit")
let coffeeTable = tabella(coffeeSales):
tabHeader "Sales of Coffee Equipment"
tabSpanner "Revenue", selRev
tabSpanner "Profit", selProf
colsLabel:
revenue_dollars: "Amount"
profit_dollars: "Amount"
revenue_pct: "Percent"
profit_pct: "Percent"
monthly_sales: "Monthly Sales"
icon: ""
product: "Product"
# formatting ----
fmtNumber endsWith("dollars"), compact = true, pattern = "${x}", nSigfig = 3
fmtPercent endsWith("pct"), decimals = 0
# style ----
tabStyle style.fill("aliceblue"), loc.body(cols = selRev)
tabStyle style.fill("papayawhip"), loc.body(cols = selProf)
tabStyle style.text(weight = "bold"), loc.body(rows = where(it["product"] == "Total"))
fmtNanoplot "monthly_sales", plotType = "bar"
fmtImage "icon", path = coffeeIcons
subMissing(missingText = "")
nbTable coffeeTable
tabelline """
`nSigfig = 3` keeps three significant figures, as Great Tables' `n_sigfig`: $904K and $2.05M.
The monthly sales are a list in each row, and a list column of a nanuq data frame becomes a
list in tabelline, which the nanoplot draws. gt's nanoplots take their values from
columns, so the R tab spreads the twelve months over twelve columns first.
"""
bookSave()
On GitHub: book/examples/examples.nim