Format teks saat mengarahkan kursor ke suatu titik dalam plotly plot

Sep 29 2020

Saya perlu mengubah teks yang ditampilkan saat pengguna mengarahkan kursor ke titik di plot kmean ini dari iris[,1]dan iris[,2]ke variabel yang dipilih misalnya Sepal.Length, Sepal.Width. Cluster harus tetap apa adanya

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)

Jawaban

1 denis Sep 29 2020 at 13:02

Anda dapat menggunakan textin aesuntuk menentukan teks yang ditampilkan, dan menambahkan tooltip = "text"ke ggplotly argumen untuk secara jelas mengatakan bahwa itu seharusnya hanya menggunakan pemetaan teks untuk tampilan penyedot debu

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)