使用ggplot和ggplotly而不是plot_ly来创建饼图

时间:2018-05-25 12:40:04

标签: r ggplot2 plotly r-plotly

您好我有一个简单的闪亮应用程序,它根据我的数据集的输入FacilityName创建一个饼图。我希望每次我选择不同的设施我的饼图来显示EXT / INT(OriginId)之间的份额。此外,每个部分都应该显示共享和“kunden”的数量。不幸的是,plot_ly()似乎对我来说不正常,所以我会使用ggplot()代替然后用ggplotly()将其转换为plotly。

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 个答案:

答案 0 :(得分: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)

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

enter image description here

支持ggplotly():

ggplotly()尚不支持极坐标。

您可以参考此github问题来遵循这一点:https://github.com/ropensci/plotly/issues/878