Plotly
allows to build quality interactive
heatmaps. This document provides several
examples with reproducible code
plotly
The plotly package allows to build interactive charts with the
plot_ly()
function. You can build heatmaps specifying
heatmap in the type
argument. You have to provide a
square matrix.
Try: to zoom, to hover, to export to png and to slide axis. Double click to re-initialize.
Note: You probably need to use the
layout()
function to increase the left margin (l
for left). Otherwise row labels will be cut.
# Load the plotly package
library(plotly)
# Data: mtcars:
data <- as.matrix(mtcars)
# basic heatmap
p <- plot_ly(x=colnames(data), y=rownames(data), z = data, type = "heatmap") %>%
layout(margin = list(l=120))
p
# save the widget
# library(htmlwidgets)
# saveWidget(p, file=paste0( getwd(), "/HtmlWidget/plotlyHeatmap1.html"))
Normalization: Plotly
does not allow to
normalize the data automatically. You need it to do it yourself.
Here is a suggestion using the apply
function. Data are
normalized by column: cell values are divided by the column mean.
Color:: Several ways are available to custom color. Here the
provided Earth
color palette is used. You can also pick
a gradient color with
colorRamp(c("red", "yellow"))
# Load the plotly package
library(plotly)
# Data: mtcars:
data <- as.matrix(mtcars)
# Normalize data
data <- apply(data, 2, function(x){x/mean(x)})
# Heatmap
p <- plot_ly(x=colnames(data), y=rownames(data),
z = data,
type = "heatmap",
colorscale= "Earth",
showscale = F) %>%
layout(margin = list(l=120))
p
# save the widget
# library(htmlwidgets)
# saveWidget(p, file=paste0( getwd(), "/HtmlWidget/plotlyHeatmap2.html"))
👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥