相关疑难解决方法(0)

我应该如何在 R 中有效地跨 gt 表的行进行格式化?

如果我想有效地格式化 gt 表的行,是否有比我在下面显示的方法更好的方法。

有些行是字符,因此不需要格式化,有些是需要一位小数的数字,有些是需要两位小数的数字,有些是需要两位小数的百分比。无论做什么,理想情况下都应该推广到其他可能的格式。

我创建了一个数据框,用于创建格式规范,但每种格式都需要在管道中使用单独的命令。

library(dplyr)
library(gt)

#create small dataset
gtcars_8 <-
  gtcars %>%
  dplyr::group_by(ctry_origin) %>%
  dplyr::top_n(2) %>%
  dplyr::ungroup() %>%
  dplyr::filter(ctry_origin != "United Kingdom")

#transpose data
row_labels <- colnames(gtcars_8)
gtcars_8_t <- as.data.frame(t(as.matrix(gtcars_8)))
gtcars_8_t$row_labels <- row_labels
my_column_names <- colnames(gtcars_8_t)[1:8]

#format data
format_specs <- as.data.frame(row_labels[1:10])
format_specs$type     <- c("c","c","n","c","c","n","n","n","n","p")
format_specs$decimals <- c( 0 , 0 , 0 , 0 , 0 , 1 , 2 , 2 , 1 , 2 )
format_specs

#make basic gt table
gtcars_8_t %>%
  slice(1:10) %>% 
  gt()

#make …
Run Code Online (Sandbox Code Playgroud)

format r gt

6
推荐指数
1
解决办法
623
查看次数

R中表格的条件格式......更好的方法?

试图改进此代码。我所做的工作有效,但看起来很丑,而且非常笨拙。

寻找 ggplot 方法或对用户更友好的方法。将不胜感激的提示和建议。

library("dplyr")
thi <- data.frame(RH    = c(1,1,1,2,2,2,3,3,3), T = c(1,2,3,1,2,3,1,2,3), THI = c(8,8,5,7,5,10,5,8,7))
table_thi <- tapply(thi$THI, list(thi$RH, thi$T), mean) %>% as.table()

x = 1:ncol(table_thi)
y = 1:nrow(table_thi)
centers <- expand.grid(y,x)

image(x, y, t(table_thi),
  col = c("lightgoldenrod", "darkgoldenrod", "darkorange"),
  breaks = c(5,7,8,9),
  xaxt = 'n', 
  yaxt = 'n', 
  xlab = '', 
  ylab = '',
  ylim = c(max(y) + 0.5, min(y) - 0.5))

text(round(centers[,2],0), round(centers[,1],0), c(table_thi), col= "black")

mtext(paste(attributes(table_thi)$dimnames[[2]]), at=1:ncol(table_thi), padj = -1)
mtext(attributes(table_thi)$dimnames[[1]], at=1:nrow(table_thi), side = 2, las …
Run Code Online (Sandbox Code Playgroud)

r conditional-formatting ggplot2

2
推荐指数
1
解决办法
772
查看次数

标签 统计

r ×2

conditional-formatting ×1

format ×1

ggplot2 ×1

gt ×1