简体   繁体   中英

create R highchart table with sparkline

I am trying to use R to create a datatable with sparklines and use it for my shiny dashboard. A simplified test data as following:

#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)

How can I make the df into something like this(instead of java, use R codes): https://www.highcharts.com/demo/sparkline

More specifically:

  1. Each person takes only one row
  2. Product_Type is spread to columns
  3. Project_Status marked by color "fill"

I used tableau to create my expected chart, hope this would help better understand my question: 在此输入图像描述

Thank you all for any helps!

finally figure out some way to accomplish it.

The basic idea in this case is to:

  1. Transform the data frame in which each value cell is a list including all the levels of the Project_Status, complete the blank with zeros.
  2. Spread the value cell using key of Product_type.
  3. Configuration on DT ColumDef and fnCallback using a little bit of java code
  4. Draw the table.

Code as below:

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 

Output: 在此输入图像描述 This also work well on my real data(400,000+ rows).

Hope this is helpful to whom in need. :)

We can have similar graph by using library (ggplot2) and facet_grid to have a seperate bar chart for each prodcut_type . and you can put Employee_name on the x-axis and Proj_count_byTypeStatus on the y-axis

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)

and the output will be:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM