R中的选择性直方图

Selective histogram in R

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

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

以防以后有人需要。

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