首页 > 解决方案 > 使这个树状图工作需要什么?

问题描述

一直试图让树形图工作,但输出是空白的,不知道为什么。如果我从网上获取一个示例并将其放入我的代码中,则输出工作正常 - 但是当我尝试使用我的数据时它不会输出。

示例代码是..

library(plotly)

labels = c("A1", "A2", "A3", "A4", "A5", "B1", "B2")
parents = c("", "A1", "A2", "A3", "A4", "", "B1")
values = c("11", "12", "13", "14", "15", "20", "30")

fig <- plot_ly(
  type="treemap",
  labels=labels,
  parents=parents,
  values=values,
  marker=list(colorscale='Reds'))

fig

我使用的数据源是数据框,当我将数据源应用于上述示例时,它根本不起作用。所以,我只能假设它是我的数据框,我不知道为什么,因为我已经尝试了我所知道的一切。

我的数据代码..

Continents_tmap <- df1 %>%
    group_by(Continent,Country.x) %>%
    summarise(Deaths = sum(Deaths))## %>%

Continents_tmap <- data.frame(Continents_tmap)

labels <- Continents_tmap$Country.x
parents <- Continents_tmap$Continent
values <- Continents_tmap$Deaths

我的用户界面代码..

tabPanel( "TreeMap",  plotlyOutput("tmap1")
                        )

我的服务器端代码....

output$tmap1 <- renderPlotly({
        plot_ly(
        type="treemap",
        labels=labels,
        parents=parents,
        values=values,
        marker=list(colorscale='Reds'))
    })

问候

我让它使用 highchart 工作,但仍然没有在情节上 - 我的 highchart 代码是

 output$tmap1 <- renderHighchart({
        Continents_tmap %>%
            filter(Continent == input$continent_2) %>%
            group_by(Country.x) %>%
            summarise(pts = sum(Deaths), fgpct = sum(Deaths)) %>%
            hchart("treemap",
                   hcaes(
                       name = Country.x,
                       value = pts,
                       color = fgpct
                   )) %>%
            hc_title(text = "Continent- Country Deaths") %>%
            hc_subtitle(text = "Deaths Rating")
        
    })
    

标签: rshinyplotly

解决方案


推荐阅读