2017-02-16 129 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