Tuesday, July 30, 2019

r - Change hover over values in a plotly plot



I have the following Shiny Application:



library(shiny)
library(plotly)

ui <- fluidPage(

plotlyOutput("plot")
)

server <- function(input, output) {

# renderPlotly() also understands ggplot2 objects!
output$plot <- renderPlotly({
plot_ly(mtcars, x = ~mpg, y = ~wt)
})


}

shinyApp(ui, server)


If I now hoover over a point I get values like: (14.5, 17.3). Is there an easy way to make sure these values appear as:



mpg: 12.3 [enter]
wt: 45.2


Answer




I believe the following does what you want:



library(shiny)
library(plotly)

ui <- fluidPage(
plotlyOutput("plot")
)

server <- function(input, output) {


# renderPlotly() also understands ggplot2 objects!
output$plot <- renderPlotly({
plot_ly(mtcars,
x = ~mpg,
y = ~wt,
hoverinfo="text",
text = ~paste0("mpg: ", mpg, "\nwt: ", wt))
})


}

shinyApp(ui, server)


enter image description here



Hope this helps!


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...