按行在gt中突出显示

huangapple go评论102阅读模式
英文:

Highlight by row in gt

问题

我正在尝试查看是否可以按行高亮显示每行中的最大值。因此,使用以下数据集:

  1. dat <- iris |>
  2. dplyr::group_by(Species) |>
  3. dplyr::summarise(
  4. Sepal.Length = mean(Sepal.Length),
  5. Sepal.Width = mean(Sepal.Width),
  6. Petal.Length = mean(Petal.Length),
  7. Petal.Width = mean(Petal.Width)
  8. ) |>
  9. tidyr::pivot_longer(
  10. cols = -Species,
  11. names_to = "Measurement",
  12. values_to = "Value"
  13. ) |>
  14. tidyr::pivot_wider(
  15. names_from = Species,
  16. values_from = Value
  17. )
  18. dat
  19. #> # A tibble: 4 × 4
  20. #> Measurement setosa versicolor virginica
  21. #> <chr> <dbl> <dbl> <dbl>
  22. #> 1 Sepal.Length 5.01 5.94 6.59
  23. #> 2 Sepal.Width 3.43 2.77 2.97
  24. #> 3 Petal.Length 1.46 4.26 5.55
  25. #> 4 Petal.Width 0.246 1.33 2.03

现在我知道可以按列进行高亮显示,如下所示:

  1. gt(dat) |>
  2. tab_style(
  3. style = list(
  4. cell_borders(
  5. color = "#000000",
  6. weight = px(2)
  7. )
  8. ),
  9. locations = cells_body(
  10. columns = setosa,
  11. rows = setosa == max(setosa)
  12. )
  13. )

这会生成如下图所示的结果:

按行在gt中突出显示

但是是否有一种方法可以按行高亮显示最大值?基本上在这里,我想要在具有最大测量值的物种周围绘制一个框。

英文:

I am trying to see if gt can highlight by rows the max value in that row. So take this dataset:

  1. dat <- iris |>
  2. dplyr::group_by(Species) |>
  3. dplyr::summarise(
  4. Sepal.Length = mean(Sepal.Length),
  5. Sepal.Width = mean(Sepal.Width),
  6. Petal.Length = mean(Petal.Length),
  7. Petal.Width = mean(Petal.Width)
  8. ) |>
  9. tidyr::pivot_longer(
  10. cols = -Species,
  11. names_to = "Measurement",
  12. values_to = "Value"
  13. ) |>
  14. tidyr::pivot_wider(
  15. names_from = Species,
  16. values_from = Value
  17. )
  18. dat
  19. #> # A tibble: 4 × 4
  20. #> Measurement setosa versicolor virginica
  21. #> <chr> <dbl> <dbl> <dbl>
  22. #> 1 Sepal.Length 5.01 5.94 6.59
  23. #> 2 Sepal.Width 3.43 2.77 2.97
  24. #> 3 Petal.Length 1.46 4.26 5.55
  25. #> 4 Petal.Width 0.246 1.33 2.03

Now I know I can highlight by column like this:

  1. gt(dat) |>
  2. tab_style(
  3. style = list(
  4. cell_borders(
  5. color = "#000000",
  6. weight = px(2)
  7. )
  8. ),
  9. locations = cells_body(
  10. columns = setosa,
  11. rows = setosa == max(setosa)
  12. )
  13. )

which produces:

按行在gt中突出显示

But if there any way to highlight the max by row? Basically here I want a box drawn around which species has the max of the measurements.

答案1

得分: 2

你可以使用一个 cells_body 列表:

  1. gt(dat) |>
  2. tab_style(style = list(cell_borders(color = "#000000",
  3. weight = px(2))),
  4. locations = lapply(
  5. 1:nrow(dat),
  6. FUN =
  7. function(rowNumber) {
  8. cells_body(columns = which.max(dat[rowNumber, ]),
  9. rows = rowNumber)
  10. }
  11. ))
英文:

You can use a list of cells_body :

  1. gt(dat) |>
  2. tab_style(style = list(cell_borders(color = "#000000",
  3. weight = px(2))),
  4. locations = lapply(
  5. 1:nrow(dat),
  6. FUN =
  7. function(rowNumber) {
  8. cells_body(columns = which.max(dat[rowNumber, ]),
  9. rows = rowNumber)
  10. }
  11. ))

huangapple
  • 本文由 发表于 2023年6月16日 01:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定