Отформатируйте текст при наведении курсора на точку в plotly plot

Sep 29 2020

Мне нужно , чтобы изменить текст , отображаемый при наведении указателя мыши на точку в этом kmeans участке от iris[,1]и iris[,2]к выбранным переменным, например Sepal.Length, Sepal.Width. Кластер должен оставаться как есть

library(plotly)
library(shiny)
library(ggplot2)

vars <- setdiff(names(iris), "Species")

ui <- pageWithSidebar(
  headerPanel('Iris k-means clustering'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', vars),
    selectInput('ycol', 'Y Variable', vars, selected = vars[[2]]),
    numericInput('clusters', 'Cluster count', 3, min = 1, max = 9)
  ),
  mainPanel(
    plotlyOutput('plot1')
  )
)
#server.r
server <- function(input, output, session) {
  
  
  
  output$plot1 <- renderPlotly({ # Combine the selected variables into a new data frame iris<-iris[, c(input$xcol, input$ycol)] cls <- kmeans(x = iris, centers = input$clusters)
    iris$cluster <- as.character(cls$cluster)
    
    ggplotly(ggplot() +
               geom_point(data = iris, 
                          mapping = aes(x = iris[,1], 
                                        y = iris[,2], 
                                        colour = cluster))+
               scale_x_discrete(name =as.character(input$xcol))+ scale_y_discrete(name =as.character(input$ycol))+
               theme_light()+
               geom_text(mapping = aes_string(x = cls$centers[, input$xcol], 
                                              y = cls$centers[, input$ycol],
                                              label = 1:input$clusters),
                         color = "black", size = 4))
    
  })
  
}

shinyApp(ui, server)

Ответы

1 denis Sep 29 2020 at 13:02

Вы можете использовать textin aesдля определения отображаемого текста и добавить tooltip = "text"к ggplotly аргументам, чтобы явно указать, что он должен просто использовать отображение текста для отображения наведения

library(plotly)
library(shiny)
library(ggplot2)

vars <- setdiff(names(iris), "Species")

ui <- pageWithSidebar(
  headerPanel('Iris k-means clustering'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', vars),
    selectInput('ycol', 'Y Variable', vars, selected = vars[[2]]),
    numericInput('clusters', 'Cluster count', 3, min = 1, max = 9)
  ),
  mainPanel(
    plotlyOutput('plot1')
  )
)
#server.r
server <- function(input, output, session) {
  
  
  
  output$plot1 <- renderPlotly({ # Combine the selected variables into a new data frame iris<-iris[, c(input$xcol, input$ycol)] cls <- kmeans(x = iris, centers = input$clusters)
    iris$cluster <- as.character(cls$cluster)
    
    ggplotly(ggplot() +
               geom_point(data = iris, 
                          mapping = aes(x = iris[,1], 
                                        y = iris[,2], 
                                        colour = cluster,
                                        text = paste0("x = ",input$xcol, "\ny = ",input$ycol,
                                                      "\ncluster: ",cluster )))+
               scale_x_discrete(name =as.character(input$xcol))+ scale_y_discrete(name =as.character(input$ycol))+
               theme_light()+
               geom_text(mapping = aes_string(x = cls$centers[, input$xcol], 
                                              y = cls$centers[, input$ycol],
                                              label = 1:input$clusters),
                         color = "black", size = 4),tooltip = "text")
    
  })
  
}

shinyApp(ui, server)