在Shiny和ggplotly上悬停其他信息

时间:2019-07-09 01:59:41

标签: r shiny plotly ggplotly

我一直在这个站点上冲浪,以查找有关Shiny和Plotly(ggplotly)的问题的答案,但是这些还不够。我正在尝试自定义ggplotly的悬停框文本。当您将鼠标悬停在绘图的任意点上时,我会在该框文本上添加更多变量。

我正在尝试使用此解决方案
Any way to display data frame information besides x and y on hover with shiny/ggplot?  而这个:Shiny: Interactive ggplot with Vertical Line and Data Labels at Mouse Hover Point

没有成功的结果

我正在尝试为足球数据创建可视化,我有一个巨大的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:姓氏

enter image description here

2 个答案:

答案 0 :(得分:0)

感谢斯蒂芬, 但这对我不起作用,正如我在mi最初发布时所说。在使用Shiny时,我需要使用aes_string设置变量(用户可以选择要可视化的变量)。因此,目前我的代码如下:

training_endog

具体地说,我将设置CA变量,无论用户选择了哪个变量,都应显示该变量。 GGplot由称为a的反应性数据构建,如您在我的代码开头所见。 请提供任何提示或技巧。

答案 1 :(得分:0)

我一直这样:

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

基本上使用HTML语法用您感兴趣的所有文本(调用由用户选择的变量)创建一个新列,然后将其与ggplot中的“​​ label”参数和ggplotly中的“ tooltip”一起使用。