I am currently creating a Shiny application that displays a histogram. I would like the histogram to show two different colors: blue for values less than the third quartile and red for values greater than the third quartile. Also, I would like the user to be able to click on (or hover over) a specific bin and have a pop-up window display what type of values are contained in the histogram.
For instance, if I have a dataset with two columns one of which is a type of fruit and the other is the cost of that one particular fruit (each fruit does not necessarily cost the same), then I would like to display a histogram of the fruit prices for all fruits. I want to change the color of the histogram whenever the cost value exceeds the third quartile. The tricky part is that I would like a pop up window to show up whenever a user either clicks or hovers over the bins that displays the frequency of each fruit in that particular bin.
I feel like the color situation could be fixed using ifelse(), but I'm not exactly sure how to go about doing the pop up window as described.
The kind of pop up that I would like to show up would ideally be something like this:
Apple: 3
Banana: 2
Here is a short sample code that will hopefully help:
Fruit <- c("Apple", "Apple", "Banana", "Grape", "Orange", "Grape", "Apple", "Banana", "Banana", "Banana")
Cost <- c(rep(sample(1:9), 1), 10)
Data <- as.data.frame(cbind(Fruit,Cost))
Data$Cost <- as.numeric(Data$Cost)
library(shiny)
ui <- fluidPage(
titlePanel("Example Code"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
hist(Data$Cost, main = "Cost of Fruit", col = "skyblue", bins = sqrt(nrow(Data)))
})
}
# Run the application
shinyApp(ui = ui, server = server)