【问题标题】:R Shiny, zoomable dendrogram programR Shiny,可缩放的树状图程序
【发布时间】:2020-08-06 15:15:24
【问题描述】:

我有一个巨大的树状图,上面有数百个名字,我希望它是交互式的,以便用户可以画一个框并“放大”名字和感兴趣的树的一部分。创建它的原始代码是borrowed from here。如图:

我把它变成了一个闪亮的应用程序并借用了一些code from here 以使其可缩放。它有点工作。它运行,两个图表显示,没有给我一个错误。但它没有按预期工作。 (我将在下面发布代码)。 我的意思是,如果我只是简单地将我的情节呈现在闪亮之外,所有的名字和一切都包括在内。但是当它呈现闪亮时,我无法在名称上绘制我的框,只有树状图的线条(希望这是有道理的),并且“缩放”图被侧向转动(与 coord_cartesian 有关?什么会我改用)并且“缩放”时看起来很奇怪。最后,我不能同时看到两个图表,必须使用滚动条才能看到它们,如图:

帮助!下面的代码(如果重要,它来自一个 r 降价文档,抱歉我不能包含实际数据,有真实姓名。

Data<-Data%>%select(-`BOARD DATE`)
Data<-t(Data)
Data<-as_tibble(Data)
names(Data) <- Data %>% slice(1) %>% unlist()
Data <- Data %>% slice(-1)

cluster_dtw_h2 <- dtwclust::tsclust(t(Data), 
                                type = "h", 
                                k = 2,  
                                distance = "dtw", 
                                control = hierarchical_control(method = "complete"),
                                preproc = NULL, 
                                args = tsclust_args(dist = list(window.size = 5L)))

hclus <- stats::cutree(cluster_dtw_h2, k = 2) %>% # hclus <- cluster::pam(dist_ts, k = 2)$clustering has a similar result
  as.data.frame(.) %>%
  dplyr::rename(.,cluster_group = .) %>%
  tibble::rownames_to_column("type_col")

hcdata <- ggdendro::dendro_data(cluster_dtw_h2)
names_order <- hcdata$labels$label
hcdata$labels$label <- ""

p1 <- hcdata %>%
ggdendro::ggdendrogram(., rotate=TRUE, leaf_labels=FALSE)


ui <- fluidPage(
fluidRow(

column(width = 12, class = "well",
  h4("Left plot controls right plot"),
  fluidRow(
    column(width = 12,
      plotOutput("plot2", height = 300,
        brush = brushOpts(
          id = "plot2_brush",
          resetOnNew = TRUE
        )
      )
    ),
    column(width = 12,
      plotOutput("plot3", height = 300)
    )
  )
  )

 )
)

server <- function(input, output) {

ranges2 <- reactiveValues(x = NULL, y = NULL)

 output$plot2 <- renderPlot({
  p1
 })

output$plot3 <- renderPlot({
 p1+
  coord_cartesian(xlim = ranges2$x, ylim = ranges2$y, expand = FALSE)
 })

  # When a double-click happens, check if there's a brush on the plot.
  # If so, zoom to the brush bounds; if not, reset the zoom.
  observe({
brush <- input$plot2_brush
if (!is.null(brush)) {
  ranges2$x <- c(brush$xmin, brush$xmax)
  ranges2$y <- c(brush$ymin, brush$ymax)

} else {
  ranges2$x <- NULL
  ranges2$y <- NULL
  }
 })

}


shinyApp(ui=ui, server=server)

【问题讨论】:

    标签: r shiny dendrogram


    【解决方案1】:

    我建议使用的工具是dendextend+ggplot+plotly。

    例子:

    library(dendextend)
    library(ggplot2)
    library(plotly)
    
    
    dend <- USArrests %>%
      dist() %>%
      hclust(method = "ave") %>%
      as.dendrogram()
    dend2 <- color_branches(dend, 5)
    
    p <- ggplot(dend2, horiz = T, offset_labels = -3)
    ggplotly(p)
    

    图片:

    放大后的图片:

    这里的一个问题是文本显然没有正确对齐(它适用于 ggplot2,但不适用于 ggplotly 版本)。这是 plotly 中的一个错误,可能应该在 dendextend 和 plotly github repos 中报告。

    【讨论】:

      猜你喜欢
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2018-03-10
      相关资源
      最近更新 更多