如何通過單擊閃亮的條形圖從條形圖中獲取類別(x 軸資訊)。請參閱下面的應用程式。我想在用戶單擊圖中的一個條形時output$cut顯示cut型別。
library(shiny)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
plotOutput('diamonds', click = "click"),
textOutput('cut')
)
server <- function(input, output, session) {
output$diamonds <- renderPlot({
diamonds %>%
ggplot()
geom_bar(aes(cut))
})
output$cut <- renderText({
req(input$click)
# This isn't meaningful
x <- round(input$click$x, 2)
y <- round(input$click$y, 2)
cat("[", x, ", ", y, "]", sep = "")
# and this doesn't work either
# nearPoints(diamonds, input$click, xvar = "cut")
})
}
shinyApp(ui, server)
uj5u.com熱心網友回復:
您從中獲得的 x 值input$click$x對應于點擊的因素。這需要四舍五入為整數,可用于獲取因子名稱。
因子名稱可用于過濾diamonds資料集。
x <- round(input$click$x) ## this will return a whole number corresponding to the factor that was clicked on.
##filter for the selected cut by using levels to get the name of the factor.
selected_cut <- diamonds %>% filter(cut == levels(diamonds$cut)[x])
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315845.html
