使用 Shiny & ggplotly 悬停附加信息
hover additional information with Shiny & ggplotly
我一直在这个网站上浏览,寻找 Shiny 和 Plotly (ggplotly) 问题的答案,但是 none 这些已经足够了。我正在尝试自定义我的 ggplotly 的悬停框文本。当您将鼠标放在我的情节的任何一点上时,我会在该框文本中包含更多变量。
我已经尝试过这个解决方案
还有这个:
没有任何成功的结果
我正在尝试为足球数据创建可视化,我有这个巨大的 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 = " "))
但都没有用。
提示:西班牙语:
名字 = 名字
阿佩利多:姓氏
我一直这样做:
https://github.com/Bustami/DatoFutbol/blob/master/libertadores2019/shiny-app/server.R
基本上使用 HTML 语法创建一个包含您所有感兴趣的文本(调用用户选择的变量)的新列,然后将其与 ggplot 和 "tooltip" 中的 "label" 参数一起使用在 ggplotly 里面。
我一直在这个网站上浏览,寻找 Shiny 和 Plotly (ggplotly) 问题的答案,但是 none 这些已经足够了。我正在尝试自定义我的 ggplotly 的悬停框文本。当您将鼠标放在我的情节的任何一点上时,我会在该框文本中包含更多变量。
我已经尝试过这个解决方案
没有任何成功的结果
我正在尝试为足球数据创建可视化,我有这个巨大的 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 = " "))
但都没有用。
提示:西班牙语: 名字 = 名字 阿佩利多:姓氏
我一直这样做:
https://github.com/Bustami/DatoFutbol/blob/master/libertadores2019/shiny-app/server.R
基本上使用 HTML 语法创建一个包含您所有感兴趣的文本(调用用户选择的变量)的新列,然后将其与 ggplot 和 "tooltip" 中的 "label" 参数一起使用在 ggplotly 里面。