根据点击事件对数据框进行子集

时间:2018-11-08 18:47:06

标签: r shiny plotly

我下面有一个闪亮的应用程序,它显示带有五个条形图的堆积条形图。此条形图取决于FinalDF数据框。我想做的是,当我单击-说-第一栏“ TC”以生成下面的表格时,其中的信息仅与“ TC”(FinalDF$Name=="TC")有关。所有条都需要相同的逻辑。

library(plotly)

ui <- fluidPage(
  plotlyOutput("plot"),
  dataTableOutput("click")

)

server <- function(input, output) {
  Name<-c("DCH","DCH","DCH","DGI","DGI","DGI","LDP","LDP","LDP","RH","RH","RH","TC","TC","TC")
  Class<-c("Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap","Class1","Class2","Overlap")
  count<-c(2077,1642,460,1971,5708,566,2316,810,221,2124,3601,413,2160,1097,377)
  FinalDF<-data.frame(Name, Class,count)


    output$plot <- renderPlotly({
      # Create the stacked bar plot using ggplot()
      stackedBarPlot<- ggplot(data = FinalDF,key=key) +
        geom_col(mapping = aes(x = Name, y = count, fill = Class,reference="DB"), width = rep(0.9,5),
                 color = "black", position = position_fill(reverse = T)) +
        geom_text(size = 4, position = position_fill(reverse = T, vjust = 0.50), color = "black", 
                  mapping = aes(x = Name, y = count, fill = Class, label = round(count),Reference="DB")) +
        coord_flip() +
        scale_fill_manual(values = c('lemonchiffon', 'palegreen3', 'deepskyblue2'),breaks=c("Class1", "Overlap", "Class2"), labels = c(paste("Unique to","DB"), "Overlap", "Unique to Comparison Dataset "),
                          guide = guide_legend(label.position = 'left', label.hjust = 0, label.vjust = 0.5))


      ggplotly(stackedBarPlot)
    })
    output$click <- renderDataTable({
      d <- event_data("plotly_click")
     if (is.null(d)) "Click events appear here (double-click to clear)" else FinalDF
    })



}

shinyApp(ui, server) 

0 个答案:

没有答案