Wednesday, January 23, 2019

r - hover additional information with Shiny & ggplotly



I have been surfing around this site for answers to my problem with Shiny and Plotly (ggplotly) but none of these has been enough. I'm trying to customize hover boxtext of my ggplotly. I would to include more variables on that boxtext when you put your mouse over any point of my plot.



I have trying with this this solution
Any way to display data frame information besides x and y on hover with shiny/ggplot?
and this one: Shiny: Interactive ggplot with Vertical Line and Data Labels at Mouse Hover Point




Without any successful result



I'm working trying to create visualization for football data, I have this huge df which has the followings variables:



     [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"


this is my ui file




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")
)
)
))


and my server file



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)
})
})



Currently Im getting only default hover information, which is Label, X axis, Y axis and colour. I would like to customize and include another variables as put First Name and Last Name. Which would be something like: paste(Nombre,Apellido, sep = " ")



I tried that line inside Tooltip:




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


but didnt worked neither.



as a tip: in spanish:
Nombre = First Name
Apellido: Last Name




enter image description here


Answer



I've been doing that this way:



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



Basically create a new column with all your text of interest (calling variables choosed by user) with HTML syntax and then use it with the "label" parameter inside ggplot and "tooltip" inside ggplotly.


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...