Home > Mobile >  Using gt opt_footnote_marks get confusing result
Using gt opt_footnote_marks get confusing result

Time:11-06

I would like to use opt_footnote_marks to label a column according to another column as follows:

# packages
library("gt")
library("data.table")

# generate sample data.table
DT <- data.table(var1 = c(1:7),
                 p.value = c(0.7142599356, 0.0516123439, 0.0004532802, 0.1129728328, 0.3334458362, 0.3849460248, 0.0241492332))

# use gt to generate a gt_tib object
gt.table <- gt(data = DT) 

# label var1 according to p.value
gt.table <- 
  tab_footnote(
    data      = gt.table,       
    footnote  = "a",      
    locations = cells_body(
      columns = var1, 
      rows    = p.value >= 0.05 & p.value < 0.1)) %>%         
  tab_footnote(
    data      = .,
    footnote  = "b",
    locations = cells_body(
      columns = var1,
      rows    = p.value >= 0.01 & p.value < 0.05)) %>%
  tab_footnote(
    data      = .,
    footnote  = "c",
    locations = cells_body(
      columns = var1,
      rows    = p.value < 0.01)) %>%
  opt_footnote_marks(
    data      = .,
    marks     = c("*", "**", "***")) 

# print table as html
gt.table

However, the result was very wired. As the picture showed: First, the order of the footnote "a", "b" and "c" was not correct. Second, a row with p.value < 0.01 should be labeled with ***. In the picture, it only had **. Thank you!

result.1

After I tried @Matt's solution by adjusting the order of the footnote, (I added a comment at the line where the adjustment was made)

# generate test data.table
DT <- data.table(var1 = c(1:7),
                 var1.p.value = c(0.7142599356, 0.0516123439, 0.0004532802, 0.1129728328, 0.3334458362, 0.3849460248, 0.0241492332),
                 var2 = c(8:14),
                 var2.p.value = c(0.0696197928, 0.0440874454, 0.1603414690, 0.0007604622, 0.2863551953, 0.1418785028, 0.3724698668))

# change data.table to gt_tib object 
gt.table <- gt(data = DT) 

# add footnote by looping
for(x in c("var1")){
  
  gt.table <- 
  tab_footnote(
    data      = gt.table,       
    footnote  = "a",      
    locations = cells_body(
      columns = x, 
      rows    = DT[[paste0(x, ".p.value")]] >= 0.05 & DT[[paste0(x, ".p.value")]] < 0.1)) %>%         
  tab_footnote(
    data      = .,
    footnote  = "b",
    locations = cells_body(
      columns = x,
      rows    = DT[[paste0(x, ".p.value")]] >= 0.01 & DT[[paste0(x, ".p.value")]] < 0.05)) %>%
  tab_footnote(
    data      = .,
    footnote  = "c",
    locations = cells_body(
      columns = x,
      rows    = DT[[paste0(x, ".p.value")]] < 0.01)) %>%
  opt_footnote_marks(
    data      = .,
    marks     = c("*", "***", "**")) # change mark order according to the present of the wanted row in the column.  
}
gt.table 

As shown in result.2, var1 with var1.p.value < 0.01 were labeled with *** and var1 with 0.01 <= var1.p.value < 0.05 were labeled with **. But when I tried to do the same job to two columns, the correlation between labels and p.value changed again. result.3 As shown here, var1 or var2 with p.value < 0.01 were labeled with ** and 0.01 <= p.value < 0.05 were labeled with ***. Thanks again!

CodePudding user response:

The order of the footnotes appear as they are first present in your column. First a on row 2, then c on row 3, and b on row 7.

If you switch your labels, they will appear as you intend:

gt.table <- 
  tab_footnote(
    data      = gt.table,       
    footnote  = "a",      
    locations = cells_body(
      columns = var1, 
      rows    = p.value >= 0.05 & p.value < 0.1)) %>%         
  tab_footnote(
    data      = .,
    footnote  = "c",
    locations = cells_body(
      columns = var1,
      rows    = p.value >= 0.01 & p.value < 0.05)) %>%
  tab_footnote(
    data      = .,
    footnote  = "b",
    locations = cells_body(
      columns = var1,
      rows    = p.value < 0.01)) %>%
  opt_footnote_marks(
    data      = .,
    marks     = c("*", "**", "***")) 

  • Related