How to center align headers and sub headers in KableExtra?

65 Views Asked by At

I have this data which I need to display as a table. I am using R Markdown and the output file is a PDF.

structure(list(`Census Year` = c(2020, 2010, 2000, 1990, 1980, 
1970, 1960, 1950, 1940, 1930, 1920, 1910), Wisconsin = c(5893718, 
5686986, 5363675, 4891769, 4705767, 4417731, 3951777, 3434575, 
3137587, 2939006, 2632067, 2333860), Midwest = c(68985454, 66927001, 
64392776, 59668632, 58865670, 56571663, 51619139, 44460762, 40143332, 
38594100, 34019792, 29888542), USA = c(331449281, 308745538, 
281421906, 248709873, 226545805, 203211926, 179323175, 151325798, 
132165129, 123202660, 106021568, 92228531), `In Midwest` = c(8.54, 
8.5, 8.33, 8.2, 7.99, 7.81, 7.66, 7.72, 7.82, 7.62, 7.74, 7.81
), `In USA` = c(1.78, 1.84, 1.91, 1.97, 2.08, 2.17, 2.2, 2.27, 
2.37, 2.39, 2.48, 2.53)), row.names = c(NA, -12L), spec = structure(list(
    cols = list(`Census Year` = structure(list(), class = c("collector_double", 
    "collector")), Wisconsin = structure(list(), class = c("collector_number", 
    "collector")), Midwest = structure(list(), class = c("collector_number", 
    "collector")), USA = structure(list(), class = c("collector_number", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x00000176450db8a0>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data. Frame"))

Now there are a few things I am looking to do. I have read the documentation, but few things are still unclear. For example I need:

  1. Center all headings, subheadings, and data. (I am having a bit of a difficulty in here.
  kable() %>%
  kable_classic() %>%
  add_header_above(c(" " = 1, "Total Population" = 3, "Percentage Share" = 2),
                   align = list(c("c"), c("c", "c", "c", "c", "c")))

It is not really doing much, and while the top order headings are center aligne, the second order and the data are still right aligned. 2. I need to add (possibly auto-generated) table numbers and table title at the beginning of the table (as I have a good number of tables, hence the automation) 2. I need to provide source, sometimes as hyperlink.
3. Given the data is for population, how to add comma separator (for example 250,000)

\begin{table}[ht]
\caption{Census Population for Wisconsin, Midwest, and USA}
\label{tab:population}
\textbf{Source:} [Census Bureau](https://www.census.gov/data/tables/time-series/dec/popchange-data-text.html)
\end{table}

I am very new to markdown and your help is greatly appreciated.

Thanks!

1

There are 1 best solutions below

0
stefan On BEST ANSWER

The align= argument of add_header_above only sets the alignment of the header. To set the alignment for the subheaders and the data you could use the align= argument of kable().

To have a comma as the big or grouping mark you could use the format.args= argument of kable(). However, as this will applied to all numeric columns I convert the Census Year column to a character.

library(kableExtra)

dat |>
  as.data.frame() |> 
  within( `Census Year` <- as.character(`Census Year`)) |> 
  kable(
    align = "c",
    format.args = list(big.mark = ",")
  ) %>%
  kable_classic() %>%
  add_header_above(
    header = c(" " = 1, "Total Population" = 3, "Percentage Share" = 2),
    align = "c"
  )

enter image description here