1
votes

Hello I have a simple shiny app which creates a pie chart based on the input FacilityName of my dataset. I want every time that I choose a different facility my pie chart to display the share between EXT/INT (OriginId). Also every piece should display the share and the number of "kunden".Unfortunately plot_ly() does not seem to work properly for me so I would to use ggplot() instead and then convert it to plotly with ggplotly().

enter image description here

#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)
  })

}
1
What is the problem you are encountering? Your app seems to work.Maximilian Peters
I would like to cretae a pie chart with ggplot() instead of plot_ly but I do not know how to convert it correctlyfirmo23

1 Answers

2
votes

Pie chart in ggplot2:

Here is how you can do a pie chart in 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)

The idea is to use a barplot and switch to polar coordinates. This yields the following graph:

enter image description here

Support in ggplotly():

Polar coordinates are not yet supported by ggplotly() though.

You can refer to this github issue to follow this point: https://github.com/ropensci/plotly/issues/878