Luc
Luc

Reputation: 958

Heatmap using custom predefined colours (d3heatmap?)

I am interested in presenting hospital indicators in a heatmap-style. I am using shiny, so I like the look and feel of the interactive d3heatmap()-plots (but am open to alternatives).

For example, say I have 4 hospitals and 5 indicators. I want to plot how each hospital scores on each of the indicators, however, colouring should not depend on the actual value of the indicator, but on separately done statistical test (a value of 80% can mean 4/5, but also 800/1000, which are very different in terms of precision of the estimate), which has the following grouping:

Example data (note that the actual numbers do not make sense):

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

Doing a d3heatmap I get:

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

(screenshot) enter image description here and when I hover over it I get the actual scores of the indicator, which I am interested in. However, the colouring is now based on the actual scores of the indicators, and not on the colours in my dataframe.

How can I use the colours from my example data frame, while keeping the option of visualizing the indicator value when hovering over the plot?

Upvotes: 1

Views: 423

Answers (2)

Luc
Luc

Reputation: 958

Thanks HubertL! I expanded it to get the exact answer:

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

Addition question: Does someone know how to expand the margins when (in my case) indicator names are long???

Upvotes: 0

HubertL
HubertL

Reputation: 19544

You can simply use numbers to code for colors, then pass colors to decode using the colors argument:

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

Upvotes: 1

Related Questions