【问题标题】:Setting bar size in formattable在格式表中设置条形大小
【发布时间】:2022-01-05 09:35:58
【问题描述】:

我想知道它是否可能反转formattablecolor_bar 命令的大小,例如:

library(formattable)

df = data.frame(Ranking = c(1, 2, 3, 4, 5, 8, 10))

formattable(df, list(Ranking = color_bar("red")))

我的表格结果是:

是否有可能获得数字1 中最大的条形图和数字10 中最短的条形图?我有一个Ranking 列,其中1 数字是最佳排名。

EDIT1

对我有用的其他解决方案是省略或删除color_bar 中的数字。

EDIT2:

其他问题:如何使标题居中?

我的代码:

formattable(df, align =c("c"), list(Ranking= color_bar("red")))

【问题讨论】:

  • 你打算在 Rmarkdown 中使用它吗?
  • 是的,我正在使用 Rmarkdown 和 knitr
  • 我有个主意,如果您不着急的话,我稍后会尽力提供帮助。
  • 我做到了,一会儿;)

标签: r formattable


【解决方案1】:

试试这个解决方案:

```{r, include=FALSE, warning=FALSE}
library(formattable)

InvProp = function(x) (1.05 - ((x) / (max(x)))) #inverse proportion
#make our formattable
df = data.frame(" " = c(1, 2, 3, 4, 5, 8, 10, 15), check.names=FALSE)
table2 <- formattable(df, list(" " = color_bar("red", InvProp)))
```

#you can modify title style with html and css, as you wish
<center>
<caption>My formattable</caption>
</center>
`r table2`


补充

library(formattable)
library(kableExtra)

InvProp = function(x) (1.1 - ((x) / (max(x)))) #inverse proportion
df = data.frame(names = c(1, 2, 3, 4, 5, 8, 10), check.names=FALSE)

df %>% 
     mutate(
         "names" = color_bar("red", InvProp)(names)) %>%
    kable("html", escape = F, align = c("r"), col.names = NULL) %>%
    kable_styling("hover", full_width = F) %>%
    column_spec(1, width = "5cm", color = "white")

您可以根据需要对其进行自定义。祝你好运。

【讨论】:

  • 不错的解决方案!谢谢
  • 存在一些改变数字颜色的可能性:可能是白色??
  • @RodrigoBadilla 我做到了
  • 太棒了!感谢您的宝贵时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-25
  • 2011-07-08
  • 2020-01-21
  • 2020-08-22
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多