1

I'm new to using R and I'm trying to create a table showing a beetle inventory where are the species are grouped by subfamily. I've figured out how to set up the table in a way that I'm happy with using the groupname_col and rowname_col attributes of gt but I'm struggling with the indentation.

Right now the items under the group names are only indented what looks like less than 1 space which makes the table difficult to read, how would I go about changing the indent to ~4 spaces or so?

I doubt my code is particularly relevant here, but here's the line where I create the table:

bt <- gt(beetledata, groupname_col = "Subfamily", rowname_col = "Species")%>%
    opt_table_lines("none")%>%
    tab_stubhead("Carabid species (subfamily)")%>%
    cols_width(
        1:2 ~ px(300),
        everything() ~ px(100)
    )%>%
    cols_align(align="center", columns = -2);

Thanks!

2 Answers2

6

Within tab_style(), you can use a cell_text() style, which has an indent option. The indent is measured in pixels or percent, not spaces.

library(tidyverse)
library(gt)

tribble(
    ~genus, ~species, ~property,
    "Homo", "sapiens", 1.1,
    "Pan", "troglodytes", 1.2,
    "Pan", "verus", 1.3
) %>%
    gt(groupname_col = "genus", rowname_col = "species") %>%
    tab_stubhead(label = "Apes") %>% 
    tab_style(
        style = cell_text(align = "left", indent = px(20)),
        locations = cells_stub()
    )

Resulting table

(PS thanks danlooo for the example data.)

3

Since gt produces html, you can modify the indent using css:

library(tidyverse)
library(gt)

tribble(
  ~genus, ~species, ~property,
  "Homo", "sapiens", 1.1,
  "Pan", "troglodytes", 1.2,
  "Pan", "verus", 1.3
) %>%
  gt(groupname_col = "genus", rowname_col = "species") %>%
  tab_stubhead(label = "Apes") %>%
  cols_width(
    1:2 ~ px(300),
    everything() ~ px(100)
  )%>%
  cols_align(align="center", columns = -2) %>%
  opt_css(css = ".gt_stub { padding-left: 50px !important; }")

enter image description here

danlooo
  • 10,067
  • 2
  • 8
  • 22