使用 gt 库在 R 中将第一行的列名大文本居中对齐并加粗

问题描述 投票:0回答:1

我在 R 中有一个名为 df 的数据框,我之前在另一个关于

gt()
的问题中使用过它,并且在 R 中找到了 here .

df = structure(list(projects = c("Restaurant, Pizzeria and Pasta House Works for  New York",
                                 "London Project - New Restaurant  Project", 
                                 "Athens - Beautiful Lands for Aigaleo City Project",
                                 "Berlin City - Exhibition  near the airport with restaurants",
                                 "Pizzeria Buenos Aires New italian restaurant - pasta, tartufo and vino",
                                 "area mean value", "total mean value"), 
                    happiness.and.joyfullness = c(3.5,4, 3, 3.2, 4, 5, 3), 
                    joy.for.children = c(3, 5, 3, 4, 5,4, 4), 
                    area = c(3, 5, 3, 3, 3, 4, 4), 
                    damages.from.environment = c(2,4, 2, 3, 3, 5, 5), 
                    approach  = c(3, 1, 5,3, 5, 5, 4), 
                    expensive = c(3, 5, 3, 4, 5, 5, 5), 
                    safety.comes.first = c(5,5, 5, 5, 5, 5, 4),
                    hungry = c(3, 5, 2, 4, 5,5, 5)), 
               row.names = c(NA, -7L), 
               class = c("tbl_df", "tbl","data.frame"))

列名是非常大的文本,我想被换行。所以我找到了一个办法:

df%>%
  gt()%>%
  cols_label("happiness.and.joyfullness" = md("happiness<br />and<br />joyfullness"))%>%
  cols_label("joy.for.children" = md("joy<br />for<br />children"))%>%
  cols_label("damages.from.environment" = md("damages<br />from<br />environment"))%>%
  cols_label("safety.comes.first" = md("safety<br />comes<br />first"))

问题是我想将它们居中对齐第一行并将其加粗。

如何使用 gt 库在 R 中做到这一点?

r dataframe gt
1个回答
1
投票

首先,不需要单独的

cols_label
。您可以使用
cols_align
tab_style
来获得您想要的更改。

 df %>%
   gt() %>%
   cols_label(
     projects = "projects",
     happiness.and.joyfullness = md("happiness<br />and<br />joyfullness"),
     joy.for.children = md("joy<br />for<br />children"),
     damages.from.environment = md("damages<br />from<br />environment"),
     safety.comes.first = md("safety<br />comes<br />first")
   ) %>%
   cols_align(align = "center", columns = c(projects)) %>%
   tab_style(
     style = cell_text(weight = "bold"),
     locations = list(
       cells_column_labels(columns = c(projects)),
       cells_body(columns = c(projects))
     )
   )

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.