Gee*_*cid 7 r heatmap ggplot2 shiny ggplotly
我正在使用 Plotly 和 Shiny在 R 中构建一个交互式时间序列热图。作为此过程的一部分,我将热图值从连续格式重新编码为有序格式 - 所以我有一个热图,其中六种颜色代表特定的计数类别,而这些类别是根据聚合计数值创建的。但是,这会导致使用ggplotly(). 我已经将它追溯到tooltip()Plotly 中呈现交互框的函数。即使我只是向tooltip(). 我正在使用来自约翰霍普金斯 CSSE 存储库. 这是一个简化的热图代码,它也使用辛普森一家的颜色主题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"))
Run Code Online (Sandbox Code Playgroud)
一旦tooltip = c("text","label","label2")减少(例如到tooltip = c("text")),性能就会提高。现在,我知道延迟不是“大规模”,但我正在将其与 Shiny 应用程序集成。一旦它与 Shiny 集成并扩展了更多数据,它真的、真的、真的很慢。我什至没有显示所有变量tooltip,它仍然很慢 -当您单击“已确认”案例时,您可以在当前版本的应用程序中看到它。
有什么建议?我已经考虑替代的互动热图的包一样d3heatmap,heatmaply和shinyHeatmaply但所有这些解决方案更适用于相关热图,他们缺少的自定义选项ggplot。
如果你将它重写为“纯”情节(不进行ggplotly转换),它会快得多。甚至3000次左右。这是一个非常小的基准测试的结果:
Unit: milliseconds
expr min lq mean median uq max neval
a 9929.8299 9929.8299 9932.49130 9932.49130 9935.1527 9935.1527 2
b 3.1396 3.1396 3.15665 3.15665 3.1737 3.1737 2
Run Code Online (Sandbox Code Playgroud)
速度慢得多的原因ggplotly是它不将输入识别为热图并创建一个散点图,其中每个矩形均使用所有必要的属性单独绘制。ggplotly如果将或的结果包装plot_ly在 中,您可以查看生成的 JSON plotly_json()。
您还可以检查object.size绘图,您将看到该ggplotly对象约为4616.4 Kb,而plotly热图只有40.4 Kb大。
df_colors = data.frame(range=c(0:13), colors=c(0:13))
color_s <- setNames(data.frame(df_colors$range, df_colors$colors), NULL)
for (i in 1:14) {
color_s[[2]][[i]] <- pal_simpsons("springfield")(13)[[(i + 1) / 2]]
color_s[[1]][[i]] <- i / 14 - (i %% 2) / 14
}
plot_ly(data = confirmed, text = text) %>%
plotly::add_heatmap(x = ~as.factor(date),
y = ~reorder(`Country/Region`, `cases count`),
z = ~as.numeric(factor(confirmed$`cnt.cat`, ordered = T,
levels = unique(confirmed$`cnt.cat`))),
xgap = 0.5,
ygap = 0.5,
colorscale = color_s,
colorbar = list(tickmode='array',
title = "Cases",
tickvals=c(1:7),
ticktext=levels(factor(x = confirmed$`cnt.cat`,
levels = unique(confirmed$`cnt.cat`),
ordered = TRUE)), len=0.5),
text = ~paste0("country: ", `Country/Region`, "<br>",
"Number of cases: ", `cases count`, "<br>",
"Category: ", `cnt.cat`),
hoverinfo ="text"
) %>%
layout(plot_bgcolor='black',
xaxis = list(title = ""),
yaxis = list(title = ""));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
926 次 |
| 最近记录: |