使用 ggplot 和 ggplotly 代替 plot_ly 创建饼图

Use ggplot and ggplotly instead of plot_ly to create a pie chart

你好,我有一个简单的闪亮应用程序,它根据我的数据集的输入 FacilityName 创建一个饼图。我希望每次选择不同的设施时,我的饼图都显示 EXT/INT (OriginId) 之间的份额。此外,每件作品都应显示份额和 "kunden" 的数量。不幸的是,plot_ly() 对我来说似乎无法正常工作,所以我会改用 ggplot(),然后使用 ggplotly 将其转换为 plotly ().

#data
OriginId=c("INT","EXT","INT","INT","EXT","INT","EXT","INT")
FacilityName=c("t1","t1","t2","t2","t1","t3","t4","t5")
FacId=c("t1","t1","t2","t2","t1","t3","t4","t5")
Testdata2<-data.frame(OriginId,FacilityName,FacId)

#ui.r
library(shinydashboard)
library(plotly)
library(data.table)

dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody()
)
#
ui <- dashboardPage(skin = "black",
                    dashboardHeader(title = img(src="Logo1.jpg", height = 50, align = "left")

                    ),

                    ## Sidebar content
                    dashboardSidebar(
                      sidebarMenu(
                        menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))

                      )
                    ),

                    ## Body content
                    dashboardBody(
                      tabItems(

                        # Dashboard tab
                        tabItem(tabName = "dashboard",
                                fluidRow(
                                  box(title = "Verhältnis interner / externer Aufträge", status = "primary", solidHeader = TRUE,
                                      plotlyOutput("pie",height = 250)),
                                  uiOutput("var")

                                )

                        )



                      )
                    )
)
#server.r
server <- function(input, output,session) {

  # Auftrag INT vs Ext
  output$pie<-renderPlotly({

    data <- dplyr::tbl_df(subset(Testdata2,Testdata2$FacilityName %in% input$variable))
    ttestdata <- data.frame(data %>% group_by(OriginId) %>% mutate(Counts = n())) 
    p <- plot_ly(data, labels=data$OriginId, values = table(data$OriginId), type = 'pie',
                 textposition = 'inside',
                 insidetextfont = list(color = '#FFFFFF'),
                 hoverinfo = 'text',
                 text = ~paste(ttestdata$Counts, ' Kunden'),
                 marker = list(
                   line = list(color = '#FFFFFF', width = 1)),
                 showlegend = FALSE) %>%
      layout(
        title = paste(input$variable),
        xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
        yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

  })



  output$var<-renderUI({
    selectInput("variable",
                h4("Abteilung wählen:"),
                choices = Testdata2 %>% distinct(FacilityName),selected = 1)
  })

}

ggplot2 中的饼图:

这是在 ggplot2 中制作饼图的方法:

data <- Testdata2 %>% filter(FacilityName == "t1")
p <- ggplot(data, aes(x = '', fill = OriginId)) +
        geom_bar(width = 1, stat = "count") +
        coord_polar(theta = "y", start = 0)

想法是使用条形图并切换到极坐标。 这会产生下图:

ggplotly() 中的支持:

不过 ggplotly() 尚不支持极坐标。

你可以参考这个github问题来了解这一点:https://github.com/ropensci/plotly/issues/878