2017-02-16 196 views
1

我有興趣以熱圖模式呈現醫院指標。我使用閃亮的,所以我喜歡互動d3heatmap() - 圖(但我可以選擇)的外觀和感覺。使用自定義預定義顏色的熱圖(d3heatmap?)

例如,假設我有4家醫院和5個指標。我想繪製每個醫院在每個指標上的得分情況,然而,着色不應該取決於指標的實際價值,而應該根據單獨完成的統計檢驗(80%的數值可能意味着4/5,而800/1000,這是在的估計的精度),其具有以下分組方面非常不同:

  • 高於平均
  • 不低於平均

實施例數據從平均

  • 差(注意實際的數字沒有意義):

    df <- data.frame(Hospital=rep(LETTERS[10:13], each=5), 
           Indicator=rep(LETTERS[1:5], 4), 
           Value=sample(1:10, 20, replace=T), 
           Conclusion=sample(c("above", "not different", "below"), 20, replace=T)) 
    df$colour[df$Conclusion == "above"] <- "green" 
    df$colour[df$Conclusion == "not different"] <- "grey" 
    df$colour[df$Conclusion == "below"] <- "red" 
    df 
    

    做一個d3heatmap我得到:

    d1 <- dcast(df, Hospital ~ Indicator, value.var = "Value") 
    row.names(d1) <- paste0("hosp",d1[[1]]) 
    d3heatmap(d1[-1], dendrogram = "none") 
    

    (截圖) enter image description here ,當我將鼠標懸停在它我得到的指示,這是我很感興趣的實際得分然而,。着色現在基於指標的實際分數,而不是基於數據框中的顏色。

    如何在使用示例數據框中的顏色的同時保持懸停在圖上時可視化指標值的選項?

  • 回答

    0

    感謝HubertL!我擴大它得到確切的答案:

    # Cast to get the matric with the values to display when hovering 
    d1 <- dcast(df, Hospital ~ Indicator, value.var = "Value") 
    row.names(d1) <- paste0("hosp",d1[[1]]) 
    
    
    # Cast to get the matrix with the colours to display 
    df$colour[df$Conclusion == "above"] <- 1   #green 
    df$colour[df$Conclusion == "not different"] <- 2 #grey 
    df$colour[df$Conclusion == "below"] <- 3   #red 
    df$colour <- as.numeric(df$colour) 
    d2 <- dcast(df, Hospital ~ Indicator, value.var = "colour") 
    
    
    # Plot heatmap using colours, and refer to the value-matrix in the 'cellnote' 
    d3heatmap(d2[-1], dendrogram = "none", colors=c("blue", "grey","red"), cellnote = d1[-1]) 
    

    加成問題:是否有人知道如何擴大利潤時,(對我來說)指標名稱較長???

    1

    你可以簡單地用數字來代碼顏色,然後通過顏色使用colors參數解碼:

    df$colour[df$Conclusion == "above"] <- 1   #green 
    df$colour[df$Conclusion == "not different"] <- 2 #grey 
    df$colour[df$Conclusion == "below"] <- 3   #red 
    
    d1 <- dcast(df, Hospital ~ Indicator, value.var = "colour") 
    
    d3heatmap(d1[-1], dendrogram = "none", colors=c("green", "grey","red")) 
    

    enter image description here

    +0

    解決了顏色問題。那很棒。但是有沒有辦法保持指標的實際價值(df $ Value),以便在懸停時顯示? – Luc

    +0

    我剛纔看到了使用cellnote ....!您可以參考實際值的不同矩陣!所以沿着:d3heatmap(d2 [-1],dendrogram =「none」,colors = c(「blue」,「gray」,「red」),cellnote = d1 [-1])。我在編輯中加入這個 – Luc