I am trying to create smooth transitions for an animated heatmap using geom_tile()
and ggplotly()
. Even after playing with the frame
, transition
, and redraw
arguments, I was not able to get the heatmap to transition smoothly between frames without flickering on and off. When setting redraw = FALSE
, the heatmap disappears after the first frame. If anyone knows how to solve this issue it would be much appreciated. Here is a minimum reproducible example that should illustrate the issue:
library("ggplot2")
library("heatmaply")
library("dendextend")
# install.packages("devtools")
library("devtools")
# devtools::install_github("DataSlingers/clustRviz")
library("clustRviz")
library("dplyr")
if(!exists("cbass_fit")){
cbass_fit <- CBASS(presidential_speech)
}
GAMMA_GRID <- seq(0, 1, length.out = 21)
build_heatmap_plot <- function(fit){
plot_data <- lapply(seq_along(GAMMA_GRID), function(g_ix) {
g <- GAMMA_GRID[g_ix]
u_hat <- get_clustered_data(fit, percent = g)
tibble::tibble(
z = as.vector(u_hat),
x = as.vector(col(u_hat)),
y = as.vector(row(u_hat)),
frame = as.vector(g_ix)
)
})
plot_data <- dplyr::bind_rows(plot_data)
X <- fit$X
g <- ggplot(data = plot_data,
aes(x = x, y = y, frame = ~frame)) +
geom_raster(aes(fill = z)) +
scale_fill_gradient2() +
theme(axis.text.y = element_text(size = 6))
p <- ggplotly(g) %>%
animation_opts(
frame = 500,
transition = 0,
redraw = TRUE
) %>%
layout(
xaxis = list(
tickvals = seq_len(NCOL(X)), ticktext = colnames(X),
linecolor = "#ffffff",
range = c(0.5, NCOL(X) + 0.5),
showticklabels = TRUE
),
yaxis = list(
tickvals = seq_len(NROW(X)), ticktext = rownames(X),
linecolor = "#ffffff",
range = c(0.5, NROW(X) + 0.5),
showticklabels = TRUE
)
)
p
}
heatmap <- build_heatmap_plot(cbass_fit)
heatmap