我有以下数据集:
structure(list(date = c("2021-04-12", "2021-04-13", "2021-04-14",
"2021-04-15", "2021-04-16", "Avg", "Change", "Change (%)"
), Cars = c(12, 13, 123, 123, 13, 31, 6, 4.6
), BIDS = c(132, 1321, 312, 123, 13, 6, 13.4, 4.1),
ASKS = c(1, 44, 21, 11, 3, 3, -10.6, -3.3)), row.names = c(NA, -8L), class = "data.frame")
我希望等于"Change", "Change (%)"
indate
列的行在正值时用绿色突出显示,如果为负值则用红色突出显示(date
不应突出显示列)。
我尝试了以下方式,但是出现了错误:
df%>% gt() %>% #rowname_col = "row", groupname_col = "group"
tab_spanner(label = "Orders", columns = matches("ASKS|BIDS")) %>%
tab_header(title = md("mY TABLE")) %>%
tab_style(
style = cells_styles(
bkgd_color = "green",
text_style = "bold"),
locations = cells_data(
columns = vars(ASKS,BIDS,CARS),
rows = vars(ASKS,BIDS,CARS) <= 0
)
)
df %>% gt() %>% #rowname_col = "row", groupname_col = "group"
tab_spanner(label = "Orders", columns = matches("ASKS|BIDS")) %>%
tab_header(title = md("mY TABLE")) %>%
tab_style(
style = list(
cell_fill(color = "green"),
cell_text(weight = "bold")
),
locations = cells_body(
columns = vars(ASKS,BIDS,CARS),
rows = 7:8 >= 0
)
)
我还尝试了一个函数,然后使用它添加了每个列的颜色。
highlight<-function(column){
tab_style(
style = list(
cell_fill(color = "green"),cell_text(weight = "bold")
),
locations = cells_body(
columns = vars(column), coloring all columns
rows = 6+which(column[7:8]>=0))
) %>%
tab_style(
style = list(
cell_fill(color = "red"),cell_text(weight = "bold")
),
locations = cells_body(
columns = vars(column), coloring all columns
rows = 6+which(column[7:8]<0))
)
}
df%>% gt() %>% #rowname_col = "row", groupname_col = "group"
tab_spanner(label = "Orders", columns = matches("ASKS|BIDS")) %>%
tab_header(title = md("mY TABLE")) %>%
highlight(ASKS) %>%
highlight(BIDS) %>%
highlight(Cars)