2017-07-24 73 views
1

我想使用[R創建數據表迷你,並用它爲我閃亮的儀表板。 簡化測試數據如下:創建[R highchart表火花

#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標記爲顏色「填充」

我用畫面來創建我的預期圖,希望這將有助於更好地理解我的問題: enter image description here

謝謝大家的任何幫助!

+0

任何提示將不勝感激,你不必完成編碼。謝謝!! – Daisywang

回答

1

終於想出一些方法來完成它。

在這種情況下的基本思想是:

  1. 變換所述數據幀,其中,每個值單元是包括Project_Status的所有級別的列表,完成空白零。
  2. 使用Product_type的鍵傳播值單元。
  3. 在DT ColumDef和fnCallback上使用一點點java代碼進行配置
  4. 繪製表格。

代碼如下:

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 

輸出: enter image description here 這也對我的真實數據(400,000行)正常工作。

希望這對有需要的人有幫助。 :)

0

我們可以通過使用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) 

和輸出將是:

enter image description here

+0

嗨,謝謝你的努力。我起初使用過ggplot方法,但後來我意識到數據表應該是最好的解決方案,因爲在真實數據集中有超過1萬個觀測值,這使x軸變得非常擁擠。因此我正在尋找Data Table Sparkline的方式來做到這一點。令人遺憾的是,大多數Sparkline示例都是用java編寫的,我不太熟悉... – Daisywang

+0

所以,你需要提供一個可重複的例子。 – MFR

+0

請檢查下面,如果你仍然感興趣。 – Daisywang