firmo23
firmo23

Reputation: 8404

Format the text when hovering over a point in plotly plot

I need to change the text displayed when the user hovers over a point in this kmeans plot from iris[,1] and iris[,2] to the variables selected for example Sepal.Length, Sepal.Width. The cluster should stay as it is

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)

Upvotes: 1

Views: 182

Answers (1)

denis
denis

Reputation: 5673

You can use text in aes to define the displayed text, and add tooltip = "text" to the ggplotly arguments to explicitely says that it should just use the text mapping for the hoovering display

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)

Upvotes: 1

Related Questions