如何将 ggplotly() 与“大”数据和 geom_tile() 一起使用

问题描述 投票:0回答:1

我尝试在 R 中每周每分钟可视化事物的状态。Plotly 的 ggplot 存在问题 - 它无法渲染它。您有解决方法的建议吗?

library(dplyr)
library(lubridate)
library(ggplot2)
library(plotly)   

data <- tibble(ts = seq(as.POSIXct("2024-01-07 00:00:00", tz="GMT"), 
                        length.out=10080, by='1 min'),
               status = sample(x = c("Status A", "Status B", "Status C"), 
                               prob = c(.6, .3, .1),
                               size = 10080, 
                               replace = TRUE)
) |> 
  dplyr::mutate(date = lubridate::as_date(ts)) |> 
  dplyr::mutate(hm = format(ts, "%H:%M")) |> 
  dplyr::select(-ts)


gg_heatmap <- ggplot2::ggplot(data = data, ggplot2::aes(x = date, y = hm, fill = status)) +
  ggplot2::geom_raster()

gg_heatmap

plotly::ggplotly(gg_heatmap) # That doesn't work anymore!

创建于 2024-01-19,使用 reprex v2.1.0

提前致谢!

r ggplot2 plotly
1个回答
0
投票

请总结您的数据或尝试进行其他可视化。 Ggplotly 必须构建 10.080 个反应字段,这很重。

我尝试用

heatmaply
绘制你的图,问题是你必须更改数值变量中的 status 并将你的数据作为矩阵进行变异。

渲染需要等待一段时间,但可以使用。

install.packages("heatmaply")
library(heatmaply)

data %>% 
  mutate(status = sapply(data$status, function(x) switch(x,
                                                         "Status A" = 1,
                                                         "Status B" = 2,
                                                         "Status C" = 3))) %>% 
  pivot_wider(names_from = date, values_from = status) %>%
  as.data.frame() %>% 
  tibble::column_to_rownames("hm") %>% 
  as.matrix() %>% 
  heatmaply(., 
            dendrogram = "none",
            xlab = "", ylab = "", 
            main = "",
            scale = "none",
            titleX = FALSE,
            hide_colorbar = TRUE,
            branches_lwd = 0.1,
            label_names = c("Time: ", "Date: ", "Status֫: "),
            fontsize_row = 5, fontsize_col = 5,
            labCol = colnames(mat),
            labRow = rownames(mat)
  )

© www.soinside.com 2019 - 2024. All rights reserved.