首页 > 解决方案 > R/Shiny:连接(情节)圆环图的切片

问题描述

在你的帮助下,我已经能够创建一个双甜甜圈图。我现在要添加的是内部甜甜圈切片之间的一些线条。不幸的是,我不知道如何:)

我想要实现的是能够加载将旧 ID 链接到新 ID 的 csv,然后在 plot_ly() 中动态地将其呈现为新层/跟踪。

例如,在下面的代码中,我想链接:

此处提供完整数据集,此处提供链接列表

到目前为止的代码:

library(shiny)
library(dplyr)
library(plotly)

# Define UI for application that draws a line-connected donut chart
ui <- fluidPage(
    plotlyOutput("plot"),
)

server <- function(input, output, session) {
    dtRutte3 <- read.csv("Herverkaveling.Rutte3.csv", sep=';', dec=',', fileEncoding="Windows-1252")
    
    # Read csv containing the internally connecting lines
    dtHerverkavelingen <- read.csv("Herverkavelingen.Rutte3.csv")
    
    # Filter out chapters and articles
    dtChapters <- dtRutte3 %>% 
        group_by(parents) %>%
        filter(parents=='root') %>% 
        ungroup()
    dtArticles <- dtRutte3 %>% 
        group_by(parents) %>%
        filter(parents!='root') %>% 
        ungroup()
    
    # Render the donut
    output$plot <- renderPlotly({

        fig <- plot_ly(height=1024, sort=FALSE)
        # Outer donut containing chapters
        fig <- add_trace(fig,
            hole = '0.75',
            type = 'pie',
            rotation = 0,
            textposition = 'inside',
            textinfo = 'label',
            insidetextfont = list(color = '#FFFFFF'),
            hoverinfo = 'text',
            text = ~paste(dtChapters$ids, ': ', dtChapters$labels),
            labels = dtChapters$ids,
            values = dtChapters$values,
            showlegend=FALSE,
            domain = list(x = c(0.1, 0.9), y = c(0, 1))
        )
        # Inner donut containing articles
        fig <- add_trace(fig,
            hole = '0.75',
            type = 'pie',
            rotation = 14.4, # somehow the inner donut is skewed?
            textposition = 'inside',
            textinfo = 'label',
            insidetextfont = list(color = '#FFFFFF'),
            hoverinfo = 'text',
            text = ~paste(dtArticles$ids, ': ', dtArticles$labels),
            labels = dtArticles$ids,
            values = dtArticles$values,
            showlegend=FALSE,
            domain = list(x = c(0.2, 0.8), y = c(0.1, 0.9))
        )
        fig <- fig %>% layout(title="Herverkaveling Rutte 3")
        fig

        #add_lines? add_trace? how to calculate coordinates?
    })
}
shinyApp(ui, server)

标签: rshinyplotlyr-plotlydonut-chart

解决方案


推荐阅读