I'm building an interactive time-series heatmap in R using Plotly and Shiny. As part of this process, I'm re-coding heatmap values from continuous to ordinal format - so I have a heatmap where six colours represent specific count categories, and those categories are created from aggregated count values. However, this causes a major performance issue with the speed of the creation of heatmap using ggplotly()
. I've traced it to the tooltip()
function from Plotly which renders interactive boxes. Labels data from my heatmap somehow overload this function in a way that it performs very slowly, even if I just add a single label component to the tooltip()
. I'm using a processed subset of COVID-19 outbreak data from Johns Hopkins CSSE repository. Here is a simplified heatmap code, which also uses The Simpsons colour theme from ggsci
:
#Load packages
library(shiny)
library(plotly)
library(tidyverse)
library(RCurl)
library(ggsci)
#Read example data from Gist
confirmed <- read_csv("https://gist.githubusercontent.com/GeekOnAcid/5638e37c688c257b1c381a15e3fb531a/raw/80ba9704417c61298ca6919343505725b8b162a5/covid_selected_europe_subset.csv")
#Wrap ggplot of time-series heatmap in ggplotly, call "tooltip"
ggplot_ts_heatmap <- confirmed %>%
ggplot(aes(as.factor(date), reorder(`Country/Region`,`cases count`),
fill=cnt.cat, label = `cases count`, label2 = as.factor(date),
text = paste("country:", `Country/Region`))) +
geom_tile(col=1) +
theme_bw(base_line_size = 0, base_rect_size = 0, base_size = 10) +
theme(axis.text.x = element_text(angle = 45, hjust = 1),legend.title = element_blank()) +
scale_fill_manual(labels = levels(confirmed$cnt.cat),
values = pal_simpsons("springfield")(7)) +
labs(x = "", y = "")
ggplotly(ggplot_ts_heatmap, tooltip = c("text","label","label2"))
Performance improves once tooltip = c("text","label","label2")
is reduced (for instance to tooltip = c("text")
). Now, I know that delay is not "massive", but I'm integrating this with a Shiny app. And once it's integrated with Shiny and scaled with more data, it is really, really, really slow. I don't even show all variables in tooltip
and its still slow - you can see it in the current version of the app when you click on 'confirmed' cases.
Any suggestions? I've considered alternative interactive heatmap packages like d3heatmap
, heatmaply
and shinyHeatmaply
but all those solutions are more intended for correlation heatmaps and they lack customisation options of ggplot
.