用迷你图创建 R highchart table

create R highchart table with sparkline

我正在尝试使用 R 创建带有 sparklinesdatatable 并使用它为了我闪亮的仪表板。 简化测试数据如下:

#Create lists for test df
Employee_name <- c('Alice',"Alice", "Alice",  'Brian', "Brian", "Brian","Brian",'Carol', 'Carol', 'Dan', 'Dan','Dan', 'Dan','Dan','Dan', "Lily", "Lily", "Eric", "Eric")
Product_type <- c('A', "A", "A", "A", "L",'L',"L", "A", "L", "A", "A", "A",'L', "L", "L", "R", "A", "I", "I")
Project_status <- c("Closed", "Legacy","Active","Closed", "Active", "Dropped", "Closed","Closed","Closed","Closed","Active","Dropped","Active","Closed","Dropped", "Active", "Closed", "Active", "Closed")
Proj_count_byTypeStatus <- c(2,1,1,4,12,1,4,3,2,10,3,1,3,8,1,8,1, 2,1)

#Test Data Frame
test_df <- data.frame(Employee_name, Product_type, Project_status, Proj_count_byTypeStatus)

如何将 df 变成这样的东西(而不是 java,使用 R 代码):https://www.highcharts.com/demo/sparkline

更具体地说:

  1. 每人只占一排
  2. Product_Type 分布到列
  3. Project_Status 颜色标记 "fill"

我使用 tableau 创建了我预期的图表,希望这有助于更好地理解我的问题:

感谢大家的帮助!

我们可以通过使用 library (ggplot2)facet_grid 为每个 prodcut_type 制作一个单独的条形图来获得相似的图表。您可以将 Employee_name 放在 x 轴上,将 Proj_count_byTypeStatus 放在 y 轴上

library(ggplot2)

ggplot(test_df, aes(x = Employee_name, y = Proj_count_byTypeStatus, fill = Project_status)) + 
  geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Product_type)

输出将是:

终于想出办法完成了。

本例的基本思路是:

  1. 转换数据框,其中每个值单元格都是一个列表,包括 Project_Status 的所有级别,用零完成空白。
  2. 使用 Product_type 的键展开值单元格。
  3. 使用一点点 java 代码对 DT ColumDef 和 fnCallback 进行配置
  4. 绘制 table.

代码如下:

library(DT)
library(sparkline)
library(dplyr)
library(htmlwidgets)
library(reshape2)
library(data.table)

#df preparation

sparkline_df_2 <- test_df %>%
  mutate(Project_status = as.character((Project_status), labels=c('Active', 
'Closed', 'Dropped', 'Legacy'))) %>%  
  group_by(Employee_name, Product_type) %>% 
  mutate(Prod_total= sum(Proj_count_byTypeStatus)) %>%
  complete(Project_status = c('Active', 'Closed', 'Dropped', 'Legacy'), fill = list(Proj_count_byTypeStatus = 0)) %>%
  as.data.frame() %>%
  group_by(Employee_name, Product_type) %>%
  summarize(Project_status = paste0(Proj_count_byTypeStatus, collapse = ",")) 

sparkline_df2_spread <- dcast(sparkline_df_2, Employee_name ~ Product_type)

#DT configuration

colDefs <- list(list(className = 'dt-center',targets = c(1:4), render = JS("function(data, type, full){ return '<span class=spark>' + data + '</span>' }")))

bar_string <- "type: 'bar', colorMap: ['#27AE60', '#48C9B0', '#C39BD3', '#F4D03F'], width: 50, height: 25, barWidth: 20, barSpacing:5, highlightColor: 'orange', tooltipFormat: '{{offset:levels}} : {{value}}', tooltipValueLookups: { levels: { '0': 'Active', '1': 'Closed', '2': 'Dropped', '3': 'Legacy' }}"  

sl_bar <- JS(sprintf("function (oSettings, json) { $('.spark:not(:has(canvas))').sparkline('html', {%s})}", bar_string)) 

d2 <- datatable(data.table(sparkline_df2_spread), 
  rownames = FALSE, 
  options = list(columnDefs = colDefs, 
  fnDrawCallback = sl_bar))

d2$dependencies <- append(d2$dependencies, htmlwidgets:::getDependency("sparkline"))

d2 

输出: 这也适用于我的真实数据(400,000 多行)。

希望对有需要的人有所帮助。 :)