Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add row spanner label feature #577

Closed
meh-huber opened this issue May 8, 2020 · 10 comments
Closed

Add row spanner label feature #577

meh-huber opened this issue May 8, 2020 · 10 comments

Comments

@meh-huber
Copy link

Is there a way to make spanner row labels (similar to spanner column labels, but for rows) or to re-position the row group label to the side?
gt_row_spanners

For example, data with two rows per "group" would be much cleaner with a single group label to the left of the plot than as many small tables stacked vertically (with the row group feature) or with duplicate labels (with the stub feature).

Here is a mock-up of one potential use:
gt_row_spanners_example

Filed as issue here per request from RStudio community

@rich-iannone
Copy link
Member

Thanks so much for filing the issue over here. It's a really great idea and one that will be implemented sooner than later!

@garberadamc
Copy link

Agree, this would be awesome! Looking to do the same thing.

@DrShaneBurke
Copy link

DrShaneBurke commented Mar 15, 2021

@rich-iannone and @jcheng5, happy to help if needed. I have created a branch to work on this, but any advice with your knowledge of the framework would be very helpful. Particularly I am trying to figure out if dt_spanners could include both the row and column spanners. Not sure how to track row labels, row groups, and summary rows if this approach is used though. Any thoughts/suggestions?

@nlsolutions
Copy link

nlsolutions commented May 2, 2021

+1 for this feature! I'm so impressed with gt and thought it had everything... until I looked up how to add a spanner row label. Looking forward to seeing this added!

@mcgriffiths
Copy link

Any progress on this? It would be really helpful!

@zmkram
Copy link

zmkram commented Nov 18, 2021

+1, would really benefit from this functionality as well.

@edgarakopyan
Copy link

Has this issue been resolved?

@rich-iannone
Copy link
Member

@edgarakopyan Yes, this is a new feature and is activated in tab_options() by setting row_group.as_column to TRUE. Here's an example:

library(gt)
library(tidyverse)

# Get vectors of 2-letter country codes for
# each region of Oceania
Australasia <- c("AU", "NZ")
Melanesia <- c("NC", "PG", "SB", "VU")
Micronesia <- c("FM", "GU", "KI", "MH", "MP", "NR", "PW")
Polynesia <- c("PF", "WS", "TO", "TV")

# Create a gt table based on a preprocessed `countrypops`
countrypops %>%
  filter(country_code_2 %in% c(
    Australasia, Melanesia, Micronesia, Polynesia)
  ) %>%
  filter(year %in% c(1995, 2005, 2015)) %>%
  mutate(region = case_when(
    country_code_2 %in% Australasia ~ "Australasia",
    country_code_2 %in% Melanesia ~ "Melanesia",
    country_code_2 %in% Micronesia ~ "Micronesia",
    country_code_2 %in% Polynesia ~ "Polynesia",
  )) %>%
  pivot_wider(names_from = year, values_from = population) %>%
  arrange(region, desc(`2015`)) %>%
  select(-starts_with("country_code")) %>%
  gt(
    rowname_col = "country_name",
    groupname_col = "region"
  ) %>%
  tab_header(title = "Populations of Oceania's Countries in 1995, 2005, and 2015") %>%
  tab_spanner(
    label = "Total Population",
    columns = c(`1995`, `2005`, `2015`)
  ) %>%
  fmt_number(
    columns = c(`1995`, `2005`, `2015`),
    decimals = 0,
    use_seps = TRUE
  ) %>%
  tab_options(row_group.as_column = TRUE)

row_group_as_column

Will close this issue now as this was missed when the PR was merged.

@edgarakopyan
Copy link

Cool, thanks!

@bacinf12
Copy link

bacinf12 commented Feb 1, 2023

I realize this issue is closed, but I have a need to have row group that spans across the width of the table and a spanner row that groups row labels into a merged cell. Any advice on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants