我有一列是其他两列的总和。我正在尝试获得color_bar
具有两种颜色的格式化程序,每种宽度代表各个数字。我尝试通过添加另一个color_bar
. 这个想法是大条将是总和列。然后,我只需要另一个条作为单个数字之一,并且我的条有两种颜色。
几个问题:主要是,当我在宽度块中删除变量~
并将列放在宽度块中时,R 似乎不理解引用。二、当我只是想看看是否可以有两个色块时,通过调整第二个色块的高度,只显示第二个色块。我把代码放在下面。
如果有人有任何提示、想法或解决方案,请告诉我。我愿意接受其他想法来显示两个单独的列如何汇总到总列。在我打字的时候,也许是一个馅饼迷你图?
这是代码:
#Make a formattable with a dual color bar
#Packages
library(dplyr)
library(formattable)
#Function
#Ideally, I'd like it to be a function, but can't visualize how to do it.
dualbar <- function(bar1 = "lightgray", bar2 = "lightblue",
fun = "comma", digits = 0) {
fun <- match.fun(fun)
formatter("span", x ~ fun(x, digits = digits),
style = y ~ style(
display = "inline-block",
direction = "rtl",
"border-radius" = "4px",
"padding-right" = "2px",
"background-color" = csscolor(bar1),
width = percent(proportion(as.numeric(y), na.rm = TRUE))),
style = z ~ style(
display = "inline-block",
direction = "rtl",
"border-radius" = "4px",
"padding-right" = "2px",
"background-color" = csscolor(bar2),
width = percent(proportion(as.numeric(z), na.rm = TRUE)),
height = "10px")
)
}
#Generate Data
set.seed(1234)
df <- data.frame(month = month.name[1:12],
valx = runif(12, 0, 5),
valy = runif(12, 2, 7))
df$total <- df$valx + df$valy
tab <- df %>%
formattable(list(area(row = 1:12, col = 2) ~
formatter("span", x ~ comma(x, digits = 0),
style = y ~ style(
display = "inline-block",
direction = "rtl",
"border-radius" = "4px",
"padding-right" = "2px",
"background-color" = csscolor("lightgray"),
width = percent(proportion(as.numeric(y), na.rm = TRUE))),
z ~ style(
display = "inline-block",
direction = "rtl",
"border-radius" = "4px",
"padding-right" = "2px",
"background-color" = csscolor("lightblue"),
width = percent(proportion(as.numeric(z), na.rm = TRUE)))
))) %>%
select(-valx, -valy) %>%
formattable::as.htmlwidget()
tab