【问题标题】:How to align source note to the table in a gt table?如何将源注释与 gt 表中的表对齐?
【发布时间】:2022-12-30 14:00:00
【问题描述】:

我在 pdf 文档中使用 R markdown,使用 gt() 在底部制作一个带有源注释的表格。出于某种原因,源注释未与表格居中对齐。如何使其与表格对齐?

这是我编织 PDF 文档时出现的结果,源注释在左侧,而不是与表格居中。

这是我的数据框:

test_df<-
    structure(list(Year = c("2010", "2015", "2020"), Lehigh_County = c(350008, 
359689, 374557), Northampton_County = c(297941, 300264, 312951
), Lehigh_Valley = c(647949, 659953, 687508)), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

这是将其变成 gt 表的代码:

    test_df %>% 
  gt() %>% 
  tab_header(
    title = "Lehigh Valley Population Forecast",
    subtitle = "2020 - 2050"
  ) %>% 
  cols_label(
    Lehigh_County = "Lehigh County",
    Northampton_County = "Northamp. County",
    Lehigh_Valley = "Lehigh Valley"
  ) %>% 
  tab_source_note(source_note = "U.S. Census / REMI Forecasts") %>% 
  fmt_number(columns = c(Lehigh_County, Northampton_County, Lehigh_Valley), sep_mark = ",", decimals = 0)

当我生成表格时它看起来是正确的,但是当我编织时,源注释向左移动。有什么建议吗?

【问题讨论】:

    标签: r r-markdown gt


    【解决方案1】:

    这是一个快速的 hack,让您可以继续工作而不必担心格式问题,但是如果您呈现为 html_document,您就可以“打印为 pdf”,这在紧要关头允许一些 HTML 样式。

    【讨论】:

      【解决方案2】:

      我没有这样的问题,但您可以尝试使用“tab_style”和“location”指向源注释来明确设置 source_note 的样式。

      tab_style(style = cell_text(align = "left"),locations = cells_source_notes())
      

      它应该强制 GT 重写源注释字段。 希望它有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-30
        • 2013-02-21
        • 2020-11-30
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        相关资源
        最近更新 更多