首页 > 解决方案 > 使用 Shiny 和 ggplotly 悬停附加信息

问题描述

我一直在这个网站上搜索关于 Shiny 和 Plotly (ggplotly) 问题的答案,但这些都还不够。我正在尝试自定义我的 ggplotly 的悬停框文本。当您将鼠标放在绘图的任何点上时,我会在该 boxtext 上包含更多变量。

我已经尝试过这个解决方案
除了x和y之外,还有什么方法可以在闪亮/ggplot悬停时显示数据框信息? 还有这个:闪亮:在鼠标悬停点带有垂直线和数据标签的交互式 ggplot

没有任何成功的结果

我正在尝试为足球数据创建可视化,我有这个巨大的 df,它有以下变量:

     [1] "ID"              "Nombre"          "Apellido"        "Rol"            
 [5] "Tecnica"         "Club"            "Competicion"     "Nacionalidad"   
 [9] "PrimerToque"     "Dribbling"       "Versatilidad"    "Pases"          
[13] "Centros"         "Remate"          "TiroLibre"       "Cabezazo"       
[17] "TiroLejano"      "SaqueLateral"    "Marcaje"         "Penales"        
[21] "Tacle"           "Corner"          "Aceleracion"     "Stamina"        
[25] "Fuerza"          "Agilidad"        "Balance"         "TendenciaLesion"
[29] "Salto"           "FormaNatural"    "Velocidad"       "FechaNacimiento"
[33] "AnoNacimiento"   "CA"              "PA"              "RepHome"        
[37] "RepActual"       "RepMundial"      "Consistencia"    "Altura"         
[41] "Peso"            "Posicion"  

这是我的ui文件

library("shiny")
library("shinythemes")
library("shinyWidgets")
library("shinydashboard")
library("leaflet")
library("ggplot2")
library("dplyr")
library("ggrepel")
library("plotly")

ui <- shinyUI(fluidPage(

  # Application title
  titlePanel("Graph"),       

  sidebarLayout(
    sidebarPanel(
      selectizeInput("Atributo1",  #xaxis 
                     label = "AtributoX",
                     choices = c("Centros", "Pases", "PrimerToque", "Remate", "Tecnica","Dribbling","TiroLejano",
                                 "TiroLibre", "SaqueLateral","Penales", "Corner" ,"Cabezazo", "Salto", "Aceleracion",
                                 "Stamina", "Fuerza","Agilidad","Balance","TendenciaLesion","FormaNatural","Velocidad", 
                                 "Marcaje", "Tacle","Consistencia", "Versatilidad", "CA", "RepHome"), selected ="CA"),
      selectizeInput("Atributo2",  #yaxis 
                     label = "AtributoY",
                     choices = c("Centros", "Pases", "PrimerToque", "Remate", "Tecnica","Dribbling","TiroLejano",
                                 "TiroLibre", "SaqueLateral","Penales", "Corner" ,"Cabezazo", "Salto", "Aceleracion",
                                 "Stamina", "Fuerza","Agilidad","Balance","TendenciaLesion","FormaNatural","Velocidad", 
                                 "Marcaje", "Tacle","Consistencia", "Versatilidad", "CA", "RepHome")),

      sliderInput("numero", "Numero de Jugadores a Mostrar:",
                  value = 10, min = 1, max = 50, round = TRUE, dragRange = FALSE),
      numericInput("Edad1", "Edad:", 42, value = 17),
      numericInput("Edad2", "Edad:", 42),

      sliderTextInput("Posicion","Posicion" , 
                      choices = c("GK","DL","DR","DC","DM","MC","ML","MR","AMC", "ST"), 
                      selected = c("MC"), #incase you want all values by default 
                      animate = FALSE, grid = FALSE, 
                      hide_min_max = FALSE, from_fixed = FALSE,
                      to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
                      to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
                      post = NULL, dragRange = TRUE),
      radioButtons("Color", label = "Color Por:",
                   choices = list("Club" = "Club", "Division" = "Competicion"), 
                   selected = "Club")
    ),          

    mainPanel(

      plotlyOutput("distPlot")
    )
  )
))

和我的服务器文件

server <- shinyServer(function(input, output) {

  output$distPlot <- renderPlotly({ #Plot tipo Plotly

    n <- input$numero
    if (is.null(n)) {
      n <- 1
    }

    dfjoin <- readRDS("dfjoin.rds")

    a <- reactive({
    dfjoin %>% filter(Posicion == input$Posicion) %>% filter(FechaNacimiento >= input$Edad1 & FechaNacimiento <= input$Edad2) %>% top_n(n, CA)
    })

    p <- ggplot(a(), aes_string(x = input$Atributo1, y = input$Atributo2, 
                              label = "Apellido", colour = input$Color)) + 
      geom_text(position=position_jitter(h=1,w=1), check_overlap = TRUE) +
      labs(colour= input$Color, title= paste("Grafico", input$Atributo1, "/",  input$Atributo2, sep =" ")) +
      theme(legend.position = "none")

    p <- ggplotly(p)

    print(p)
  })
})

目前我只得到默认的悬停信息,即标签、X 轴、Y 轴和颜色。我想自定义并包含另一个变量作为名字和姓氏。类似于: paste(Nombre,Apellido, sep = " ")

我在工具提示中尝试了该行:

$ggplotly(p, Tooltip = paste(Nombre,Apellido, sep = " "))

但也没有用。

作为提示:西班牙语:Nombre = 名字 Apellido:姓氏

在此处输入图像描述

标签: rshinyplotlyggplotly

解决方案


我一直这样做:

https://github.com/Bustami/DatoFutbol/blob/master/libertadores2019/shiny-app/server.R

基本上使用 HTML 语法创建一个包含所有您感兴趣的文本(调用用户选择的变量)的新列,然后将其与 ggplot 内的“标签”参数和 ggplotly 内的“工具提示”一起使用。


推荐阅读