[英]hover additional information with Shiny & ggplotly
我一直在這個站點上沖浪,以查找有關Shiny和Plotly(ggplotly)的問題的答案,但是這些還不夠。 我正在嘗試自定義ggplotly的懸停框文本。 當您將鼠標懸停在繪圖的任意點上時,我會在該框文本中包含更多變量。
我已經嘗試過這種解決方案
有什么辦法在具有光澤/ ggplot的懸停上顯示x和y以外的數據幀信息? 這個: 閃亮:鼠標懸停點帶有垂直線和數據標簽的交互式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"
這是我的用戶界面文件
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:姓
我一直這樣做:
https://github.com/Bustami/DatoFutbol/blob/master/libertadores2019/shiny-app/server.R
基本上,使用HTML語法創建一個包含您感興趣的所有文本(調用用戶選擇的變量)的新列,然后將其與ggplot內的“ label”參數和ggplotly內的“ tooltip”一起使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.