I would like for the graphs in my shiny app to change color (or be highlighted) when I hover over them with my mouse. I would also like them to only output the count of my graph, instead of the count and the x-value.
Here is what it looks like in my Shiny App:
To be clear, I would like the bar that I am hovering over to turn light blue (or have a black outline) and to only say "count: 61735" without saying "fat_infreq(race): Black".
Below, I have attached a reproducible example:
library(shiny)
library(ggplot2)
library(plotly)
library(data.table)
library(shinythemes)
library(forcats)
require(stringi)
require(stringr)
library(scales)
library(ggthemes)
library(plyr)
library(dplyr)
library(readr)
library(tidyr)
library(ggthemes)
library(forcats)
library(xtable)
library(googledrive)
library(googlesheets4)
library(gridExtra)
library(lubridate)
library(DT)
library(vroom)
library(utf8)
library(tableHTML)
library(bslib)
library(devtools)
library(readr)
library(RColorBrewer)
ethnicity <- c("Hispanic", "Non-hispanic","Hispanic","Hispanic","Hispanic","Hispanic","White","White","White","White",
"White","Hispanic","Hispanic", "Hispanic","Hispanic","Hispanic","White","White","White","White")
filtered_data <- data.frame(ethnicity)
ui <- fluidPage(
titlePanel("Example"),
mainPanel(
plotlyOutput("ethnicity_barplot")
)
)
server <- function(input, output) {
output$ethnicity_barplot <- renderPlotly({
ggplotly({
ethnicity_barplot <-ggplot(data = filtered_data, aes(x = fct_infreq(ethnicity))) +
geom_bar() +
xlab("Ethnicity") +
ylab("Number of People") +
labs(title = "Ethnicity of Defendants in New York State Courts") +
geom_bar(fill = "#327EC2") +
theme(panel.background = element_rect(fill = 'white'))+
theme(plot.background = element_rect(fill = 'white'))+
theme(plot.title = element_text(hjust = 0.5))+
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
geom_text(stat='count', aes(label=..count..), vjust = -.3)
ethnicity_barplot
})
})
}
# Run the application
shinyApp(ui = ui, server = server)
(Not all the packages are needed, just can't remember the ones that are)
library(tidyverse)
library(shiny)
library(plotly)
ethnicity <- c(
"Hispanic", "Non-hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic", "White", "White", "White", "White",
"White", "Hispanic", "Hispanic", "Hispanic", "Hispanic", "Hispanic", "White", "White", "White", "White"
)
filtered_data <- data.frame(ethnicity)
ui <- fluidPage(
titlePanel("Example"),
mainPanel(
plotlyOutput("ethnicity_barplot")
)
)
server <- function(input, output) {
output$ethnicity_barplot <- renderPlotly({
ggplotly(
p = {
ethnicity_barplot <- ggplot(data = filtered_data, aes(x = fct_infreq(ethnicity))) +
geom_bar() +
xlab("Ethnicity") +
ylab("Number of People") +
labs(title = "Ethnicity of Defendants in New York State Courts") +
geom_bar(fill = "#327EC2") +
theme(panel.background = element_rect(fill = "white")) +
theme(plot.background = element_rect(fill = "white")) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
geom_text(stat = "count", aes(label = ..count..), vjust = -.3)
ethnicity_barplot
},
# only show the y values (counts) as tooltips
tooltip = "y"
) %>%
# highlight current label
layout(hoverlabel = list(bgcolor = "orange"), hoveron = list(bgcolor = "red"))
})
}
# Run the application
shinyApp(ui = ui, server = server)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.