STAA 566 Tables

Author

Tiana Stastny

2022 Thailand Province Population Table

The purpose of this table is to display how population varies across the provinces of Thailand. The data used is from 2022.

Thailand population data for 2022

The Thailand population data came from the same ‘Humanitarian Data Exchange’ (HDX) source:

https://data.humdata.org/dataset/cod-ps-tha

Specifically, I used the ‘tha_admpop_adm1_2022.csv’ file.

Load libraries

library(tidyverse)
-- Attaching packages --------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.5     v purrr   0.3.4
v tibble  3.1.6     v dplyr   1.0.7
v tidyr   1.1.4     v stringr 1.4.0
v readr   2.1.1     v forcats 0.5.1
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
library(dplyr)
library(knitr)
library(kableExtra)
Warning: package 'kableExtra' was built under R version 4.1.3

Attaching package: 'kableExtra'
The following object is masked from 'package:dplyr':

    group_rows
library(DT)
Warning: package 'DT' was built under R version 4.1.3
library(htmlwidgets)
Warning: package 'htmlwidgets' was built under R version 4.1.3
library(htmlTable)
Warning: package 'htmlTable' was built under R version 4.1.3
library(magick)
Warning: package 'magick' was built under R version 4.1.3
Linking to ImageMagick 6.9.12.3
Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
Disabled features: fontconfig, x11
library(webshot)
Warning: package 'webshot' was built under R version 4.1.3

Load population data

Load the population data.

# read in population data from csv file

thailand_population <- read.csv("C:/Users/tiana/OneDrive/Documents/CSU_STAA_566_Data_Viz/tables-TStas/data/tha_admpop_adm1_2022.csv")

Important columns include ADM1_NAME (province name) and T_TL (total population for a district), along with F_TL (female total) and M_TL (male total).

# want the ADM1_NAME, F_TL, M_TL and T_TL columns
# Notes on levels of data:
# 0 = country
# 1 = province ** we want level 1
# 2 = district  
# 3 = sub-district

Make a new dataframe comprised of the desired columns.

# make new dataframe
province_populations <- data.frame(thailand_population$ADM1_NAME, thailand_population$F_TL, thailand_population$M_TL, thailand_population$T_TL)

# rename columns for readability
province_populations <- province_populations %>%
  rename(Province = thailand_population.ADM1_NAME,
         Female = thailand_population.F_TL,
         Male = thailand_population.M_TL,
         Total = thailand_population.T_TL)

# take a look
head(province_populations)
       Province  Female    Male    Total
1 Amnat Charoen   95551   92132   187683
2     Ang Thong  109504   96970   206474
3       Bangkok 5750142 5471549 11221691
4     Bueng Kan  147527  144017   291544
5      Buri Ram  475551  445292   920843
6  Chachoengsao  372952  361005   733957
# get dimensions
dim(province_populations)
[1] 77  4

Make a basic table

First, make a basic table using kable.

kable(province_populations)
Province Female Male Total
Amnat Charoen 95551 92132 187683
Ang Thong 109504 96970 206474
Bangkok 5750142 5471549 11221691
Bueng Kan 147527 144017 291544
Buri Ram 475551 445292 920843
Chachoengsao 372952 361005 733957
Chai Nat 115171 103013 218184
Chaiyaphum 369898 347884 717782
Chanthaburi 217156 210612 427768
Chiang Mai 917483 881140 1798623
Chiang Rai 537393 519319 1056712
Chon Buri 1125257 1144336 2269593
Chumphon 212304 217138 429442
Kalasin 321114 304505 625619
Kamphaeng Phet 428663 410871 839534
Kanchanaburi 384568 377441 762009
Khon Kaen 819888 770911 1590799
Krabi 169359 170330 339689
Lampang 305896 296658 602554
Lamphun 188123 177674 365797
Loei 211731 210619 422350
Lop Buri 352214 346467 698681
Mae Hong Son 88079 91957 180036
Maha Sarakham 320005 295561 615566
Mukdahan 182511 179232 361743
Nakhon Nayok 113318 107000 220318
Nakhon Pathom 532300 506386 1038686
Nakhon Phanom 215720 203856 419576
Nakhon Ratchasima 1181269 1134704 2315973
Nakhon Sawan 407245 377927 785172
Nakhon Si Thammarat 616177 591918 1208095
Nan 194480 192941 387421
Narathiwat 296687 293091 589778
Nong Bua Lam Phu 217228 204745 421973
Nong Khai 185477 173985 359462
Nonthaburi 1082305 984181 2066486
Pathum Thani 1343056 1271298 2614354
Pattani 276494 262932 539426
Phangnga 122048 127129 249177
Phatthalung 208077 193727 401804
Phayao 148291 142992 291283
Phetchabun 401784 385032 786816
Phetchaburi 230356 216085 446441
Phichit 236306 219337 455643
Phitsanulok 478646 449991 928637
Phra Nakhon Si Ayutthaya 526913 492552 1019465
Phrae 162593 151403 313996
Phuket 534137 538778 1072915
Prachin Buri 355016 334586 689602
Prachuap Khiri Khan 223008 224242 447250
Ranong 175548 182303 357851
Ratchaburi 364095 331953 696048
Rayong 623890 643781 1267671
Roi Et 409394 384671 794065
Sa Kaeo 282858 276016 558874
Sakon Nakhon 371664 353819 725483
Samut Prakan 1656432 1587089 3243521
Samut Sakhon 879505 879940 1759445
Samut Songkhram 75540 69357 144897
Saraburi 413155 395542 808697
Satun 131045 134819 265864
Si Sa Ket 340130 319489 659619
Sing Buri 76494 68514 145008
Songkhla 807477 763716 1571193
Sukhothai 303998 276767 580765
Suphan Buri 376506 341715 718221
Surat Thani 531229 514226 1045455
Surin 416048 386271 802319
Tak 251552 248276 499828
Trang 267261 254345 521606
Trat 129741 136144 265885
Ubon Ratchathani 796006 768311 1564317
Udon Thani 490139 467073 957212
Uthai Thani 129885 120225 250110
Uttaradit 183005 170237 353242
Yala 200610 198698 399308
Yasothon 184252 177103 361355

Now try the same table using kableExtra.

kbl(province_populations)
Province Female Male Total
Amnat Charoen 95551 92132 187683
Ang Thong 109504 96970 206474
Bangkok 5750142 5471549 11221691
Bueng Kan 147527 144017 291544
Buri Ram 475551 445292 920843
Chachoengsao 372952 361005 733957
Chai Nat 115171 103013 218184
Chaiyaphum 369898 347884 717782
Chanthaburi 217156 210612 427768
Chiang Mai 917483 881140 1798623
Chiang Rai 537393 519319 1056712
Chon Buri 1125257 1144336 2269593
Chumphon 212304 217138 429442
Kalasin 321114 304505 625619
Kamphaeng Phet 428663 410871 839534
Kanchanaburi 384568 377441 762009
Khon Kaen 819888 770911 1590799
Krabi 169359 170330 339689
Lampang 305896 296658 602554
Lamphun 188123 177674 365797
Loei 211731 210619 422350
Lop Buri 352214 346467 698681
Mae Hong Son 88079 91957 180036
Maha Sarakham 320005 295561 615566
Mukdahan 182511 179232 361743
Nakhon Nayok 113318 107000 220318
Nakhon Pathom 532300 506386 1038686
Nakhon Phanom 215720 203856 419576
Nakhon Ratchasima 1181269 1134704 2315973
Nakhon Sawan 407245 377927 785172
Nakhon Si Thammarat 616177 591918 1208095
Nan 194480 192941 387421
Narathiwat 296687 293091 589778
Nong Bua Lam Phu 217228 204745 421973
Nong Khai 185477 173985 359462
Nonthaburi 1082305 984181 2066486
Pathum Thani 1343056 1271298 2614354
Pattani 276494 262932 539426
Phangnga 122048 127129 249177
Phatthalung 208077 193727 401804
Phayao 148291 142992 291283
Phetchabun 401784 385032 786816
Phetchaburi 230356 216085 446441
Phichit 236306 219337 455643
Phitsanulok 478646 449991 928637
Phra Nakhon Si Ayutthaya 526913 492552 1019465
Phrae 162593 151403 313996
Phuket 534137 538778 1072915
Prachin Buri 355016 334586 689602
Prachuap Khiri Khan 223008 224242 447250
Ranong 175548 182303 357851
Ratchaburi 364095 331953 696048
Rayong 623890 643781 1267671
Roi Et 409394 384671 794065
Sa Kaeo 282858 276016 558874
Sakon Nakhon 371664 353819 725483
Samut Prakan 1656432 1587089 3243521
Samut Sakhon 879505 879940 1759445
Samut Songkhram 75540 69357 144897
Saraburi 413155 395542 808697
Satun 131045 134819 265864
Si Sa Ket 340130 319489 659619
Sing Buri 76494 68514 145008
Songkhla 807477 763716 1571193
Sukhothai 303998 276767 580765
Suphan Buri 376506 341715 718221
Surat Thani 531229 514226 1045455
Surin 416048 386271 802319
Tak 251552 248276 499828
Trang 267261 254345 521606
Trat 129741 136144 265885
Ubon Ratchathani 796006 768311 1564317
Udon Thani 490139 467073 957212
Uthai Thani 129885 120225 250110
Uttaradit 183005 170237 353242
Yala 200610 198698 399308
Yasothon 184252 177103 361355

We can sort by Total population, and add a ‘header above’. To improve readability, we can add commas to the numbers, and add styles of ‘striped’, ‘hover’, and ‘condensed’.

# sort by Total population, descending order
sorted_by_total <- province_populations %>% arrange(desc(Total))

# take a look
head(sorted_by_total)
           Province  Female    Male    Total
1           Bangkok 5750142 5471549 11221691
2      Samut Prakan 1656432 1587089  3243521
3      Pathum Thani 1343056 1271298  2614354
4 Nakhon Ratchasima 1181269 1134704  2315973
5         Chon Buri 1125257 1144336  2269593
6        Nonthaburi 1082305  984181  2066486
# make table
sorted_by_total_table <- kbl(sorted_by_total, format.args = list(big.mark = ",")) %>%
  add_header_above(c(" ", "Subpopulations" = 2, " "))  %>%
  kable_styling(font_size=12,
                bootstrap_options = c("striped", "hover", "condensed"),
                full_width = FALSE,
                fixed_thead = TRUE) %>%
  # save_kable("thailand_provinces_kable_extra.png") %>%
  scroll_box(width = "100%", height = "400px")

sorted_by_total_table
Subpopulations
Province Female Male Total
Bangkok 5,750,142 5,471,549 11,221,691
Samut Prakan 1,656,432 1,587,089 3,243,521
Pathum Thani 1,343,056 1,271,298 2,614,354
Nakhon Ratchasima 1,181,269 1,134,704 2,315,973
Chon Buri 1,125,257 1,144,336 2,269,593
Nonthaburi 1,082,305 984,181 2,066,486
Chiang Mai 917,483 881,140 1,798,623
Samut Sakhon 879,505 879,940 1,759,445
Khon Kaen 819,888 770,911 1,590,799
Songkhla 807,477 763,716 1,571,193
Ubon Ratchathani 796,006 768,311 1,564,317
Rayong 623,890 643,781 1,267,671
Nakhon Si Thammarat 616,177 591,918 1,208,095
Phuket 534,137 538,778 1,072,915
Chiang Rai 537,393 519,319 1,056,712
Surat Thani 531,229 514,226 1,045,455
Nakhon Pathom 532,300 506,386 1,038,686
Phra Nakhon Si Ayutthaya 526,913 492,552 1,019,465
Udon Thani 490,139 467,073 957,212
Phitsanulok 478,646 449,991 928,637
Buri Ram 475,551 445,292 920,843
Kamphaeng Phet 428,663 410,871 839,534
Saraburi 413,155 395,542 808,697
Surin 416,048 386,271 802,319
Roi Et 409,394 384,671 794,065
Phetchabun 401,784 385,032 786,816
Nakhon Sawan 407,245 377,927 785,172
Kanchanaburi 384,568 377,441 762,009
Chachoengsao 372,952 361,005 733,957
Sakon Nakhon 371,664 353,819 725,483
Suphan Buri 376,506 341,715 718,221
Chaiyaphum 369,898 347,884 717,782
Lop Buri 352,214 346,467 698,681
Ratchaburi 364,095 331,953 696,048
Prachin Buri 355,016 334,586 689,602
Si Sa Ket 340,130 319,489 659,619
Kalasin 321,114 304,505 625,619
Maha Sarakham 320,005 295,561 615,566
Lampang 305,896 296,658 602,554
Narathiwat 296,687 293,091 589,778
Sukhothai 303,998 276,767 580,765
Sa Kaeo 282,858 276,016 558,874
Pattani 276,494 262,932 539,426
Trang 267,261 254,345 521,606
Tak 251,552 248,276 499,828
Phichit 236,306 219,337 455,643
Prachuap Khiri Khan 223,008 224,242 447,250
Phetchaburi 230,356 216,085 446,441
Chumphon 212,304 217,138 429,442
Chanthaburi 217,156 210,612 427,768
Loei 211,731 210,619 422,350
Nong Bua Lam Phu 217,228 204,745 421,973
Nakhon Phanom 215,720 203,856 419,576
Phatthalung 208,077 193,727 401,804
Yala 200,610 198,698 399,308
Nan 194,480 192,941 387,421
Lamphun 188,123 177,674 365,797
Mukdahan 182,511 179,232 361,743
Yasothon 184,252 177,103 361,355
Nong Khai 185,477 173,985 359,462
Ranong 175,548 182,303 357,851
Uttaradit 183,005 170,237 353,242
Krabi 169,359 170,330 339,689
Phrae 162,593 151,403 313,996
Bueng Kan 147,527 144,017 291,544
Phayao 148,291 142,992 291,283
Trat 129,741 136,144 265,885
Satun 131,045 134,819 265,864
Uthai Thani 129,885 120,225 250,110
Phangnga 122,048 127,129 249,177
Nakhon Nayok 113,318 107,000 220,318
Chai Nat 115,171 103,013 218,184
Ang Thong 109,504 96,970 206,474
Amnat Charoen 95,551 92,132 187,683
Mae Hong Son 88,079 91,957 180,036
Sing Buri 76,494 68,514 145,008
Samut Songkhram 75,540 69,357 144,897

Use DT framework

Using the DataTables (DT) framework provides another way to generate a dynamic table, and easily allows the user to sort by any of the columns. Limiting the display to 10 provinces at a time helps with readability. Sorting by the various columns and only viewing 10 rows at a time provides the user a good overview of the data in a short amount of time (and effort!).

options(DT.options = list(pageLength = 10, language = list(search = 'Filter:')))
province_population_DT <- province_populations %>%
  select(Province, Female, Male, Total) %>% datatable(colnames=c("Province","Female","Male","Total")) %>% 
  formatRound('Female', digits = 0) %>%
  formatRound('Male', digits = 0) %>%
  formatRound('Total', digits = 0) 

province_population_DT
#save to png via saveWidget and webshot
# html <- "province.html"
# saveWidget(province_population_DT, html)
# webshot(html, "province_DT.png")

# save as html file
# saveWidget(province_population_DT,  file="thailand_province_dynamic_table.html")