2. StyleInterval and StyleColorBar
First we show how to visualize the extent of implementation support using heatmap.
#styleInterval
#styleColorBar
#install.packages('DT')
library(DT)
#Generate mock data complying with the data strucure we have; will replace this with real data later
Implement <- as.data.frame(cbind(matrix(round(runif(35,0,3),2),7)))
Consensus <- as.data.frame(cbind(matrix(round(runif(35,50,100),2),7)))
ColName <- c("Screening","Assessment","Treatment Planning","Reassessment","Linkage/Transition Planning")
RowName <- c("Use","Implementation","Protocol","Training","Practice Feedback","Outcome Feedback","Policy or System Change")
rownames(Implement) <- RowName
colnames(Implement) <- ColName
rownames(Consensus) <- RowName
colnames(Consensus) <- ColName
#Set up breaks and colors
brks <- quantile(Implement, probs = seq(.05, .95, .05), na.rm = TRUE, names = FALSE)
clrs <- round(seq(255, 40, length.out = length(brks) + 1), 0) %>%
{paste0("rgb(", .,",", .,",220)")}
#Implement heatmap with StyleInterval
datatable(Implement,options = list(
autoWidth = TRUE, columnDefs = list(list(width = '100px',targets = 1:5))
))%>%formatStyle(names(Implement),background = styleInterval(brks,clrs))
Then we show how to visualize consensus using barplot.
#Implement barplot with StyleColorBar
datatable(Consensus, options = list(
autoWidth = TRUE, columnDefs = list(list(width = '100px',targets = 1:5))
))%>% formatStyle(names(Consensus),
background = styleColorBar(range(Consensus,40), 'steelblue'),
backgroundSize = '98% 88%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center')
Finally we try to combine the two plots together where the deepening of color shows the extent of implementation support while the length of bar shows the consensus.
- Note: this implementation can be improved by someone who knows more about Javascript; the drawback of StyleColorBar is that the color of bars cannot be changed (cannot even be assigned two different colors). The way in which I implemented the combination is that instead of changing the color of bars, I mask the background color with block of complementary length.
ConsensusCompl <- 100 - Consensus
datatable(cbind(Implement,ConsensusCompl), options = list(
autoWidth = TRUE, columnDefs = list(list(width = '100px',targets = 1:5), list(visible = FALSE, targets = 6:10))
))%>%
formatStyle(names(Implement),backgroundColor = styleInterval(brks,clrs)) %>%
formatStyle(1:5,6:10,
background = styleColorBar(c(0,60), 'rgba(255, 255, 255, 0.95)',angle = -90),
backgroundSize = '100% 100%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center')
3. Plotly package
We can visualize a similar heatmap using Plotly package. However try out the new interactive feature!
#install.packages('plotly')
library(plotly)
ImplementM <- as.matrix(Implement)
#Customize the scaling
vals <- unique(scales::rescale(c(ImplementM)))
o <- order(vals, decreasing = FALSE)
cols <- scales::col_numeric("Blues", domain = NULL)(vals)
colz <- setNames(data.frame(vals[o], cols[o]), NULL)
p <- plot_ly(
x = ColName, y = RowName,
z = ImplementM, type = "heatmap",colorscale = colz
)
#Now let's see the interactive plot
p
We can also try out 3-D plot where the z-axis indicates the implementation and we use color to show consensus.
- However the plot is not as enjoyable as I thought; can think of other 3-D implementation for improvement.
ConsensusM <- as.matrix(Consensus)
p <- plot_ly( x = ~rep(RowName,5), y = ~rep(ColName,7), z = ~c(ImplementM),
marker = list(color = ~c(ConsensusM), colorscale = c('#FFE1A1', '#683531'), showscale = TRUE))%>%
add_markers() %>%
layout(annotations = list(
x = 1.13,
y = 1.05,
text = 'Consensus',
showarrow = FALSE
))
p