[英]Changing GGPLOTLY graphs interaction in R Shiny App
我希望我的 shiny 應用程序中的圖表在我用鼠標 hover 時更改顏色(或突出顯示)。 我還希望他們僅 output 我的圖形計數,而不是計數和 x 值。
需要明確的是,我希望我懸停的欄變成淺藍色(或有黑色輪廓)並且只說“計數:61735”而不說“fat_infreq(種族):黑色”。
下面,我附上了一個可重現的例子:
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)
(不是所有的包都需要,只是不記得那些包了)
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.