R中的选择性直方图

时间:2018-05-31 21:18:12

标签: r shiny plotly r-plotly

你如何在R中实现这样的东西?

利用散点图选择是困难的部分。我没有在Shiny或plotly中看到类似的东西。

1 个答案:

答案 0 :(得分:1)

如果将来有人需要它。

ui.R

library(shiny)
library(ggplot2)

shinyUI(basicPage(
  titlePanel("Number of forward gears of selected cars"),

  plotOutput("plot",brush = "plot_brush"),
  plotOutput("histo",height="200px")
))

server.R

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {
  output$plot <- renderPlot({  
    ggplot(mtcars, aes(x=wt, y=mpg,color=as.factor(gear))) + geom_point() + labs(y=  'Miles per gallon',x = 'Weight (1000 lbs)')  
  })

  output$histo <- renderPlot({  
    selected_points<-brushedPoints(mtcars, input$plot_brush, xvar = "wt", yvar = "mpg")
    ggplot(data=selected_points, aes(selected_points$gear,fill = as.factor(gear))) + geom_bar() + labs(x="Forward Gears", y="Count")   + coord_flip() +theme_minimal()    
    })
})

Result