rtstats gt table layout with spanning column

41 Views Asked by At

With gt, I would like to have a table with the following layout enter image description here

I do not know how to create the leftmost column (with value 2024 spanning all rows). Can it be done?

I am getting closer with this code:

p_df <-
  tibble(old=paste0("P",1:3),
         P1=rep(0,3),
         P2=rep(0,3),
         P3=rep(0,3),
         Jahr=2024
         )

p_df |>
  gt(rowname_col = "old",
     groupname_col = "Jahr") |>
  tab_spanner(label=2020,columns=2:4) |>
  tab_options(row_group.as_column = TRUE) |>
  tab_style(
    style=cell_text(v_align="middle"),
    locations =
      cells_row_groups()
  )

which produces

enter image description here

Now I still need to rotate the text. Can this be done?

I found a way to rotate text using opt_css

p_df |>
  gt(rowname_col = "old",
     groupname_col = "Jahr",
     id = "one") |>
  tab_spanner(label=2020,columns=2:4) |>
  tab_options(row_group.as_column = TRUE) |>
  tab_style(
    style=cell_text(v_align="middle"),
    locations =
      cells_row_groups()) |>
  opt_css(
      css = "#one .gt_row {
      text-align: center;
      transform: rotate(-90deg);
    }
    "
)

But this way text in all columns is rotated

I do not know how i can restrict opt_css to be applied onlu to the first column. Is there a way?

enter image description here

0

There are 0 best solutions below