Warning: package 'webshot' was built under R version 4.1.3
Load population data
Load the population data.
# read in population data from csv filethailand_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 dataframeprovince_populations <-data.frame(thailand_population$ADM1_NAME, thailand_population$F_TL, thailand_population$M_TL, thailand_population$T_TL)# rename columns for readabilityprovince_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 lookhead(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 dimensionsdim(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 ordersorted_by_total <- province_populations %>%arrange(desc(Total))# take a lookhead(sorted_by_total)
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!).
#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")