简体   繁体   中英

R Shiny XTS - Change name of the default tooltip using ggplot

I have a object that has dates as values, I'm using and app for show the result. But I want to change the default names of the tooltip when the mouse is on the line.

From: index: 2020-03-19 value: 70

To: Date: 2020-03-19 Cantidad: 70

Code for XTS:

data<-rnorm(10)
    dates <- seq(as.Date("2016-01-01"), length =10, by = "days")
    xtsMyData <- xts(x = data, order.by = dates)

Plot:

r <- ggplot(tidy(xtsMyData), aes(x=index,y=value, color=series, type = 'scatter', mode = 'lines')
    ) + geom_line(size=2)

The result is:默认值

I'm triyng the following code:

 r <- ggplot(tidy(xtsMyData), aes(x=index,y=value, color=series, type = 'scatter', mode = 'lines')
    ) + geom_line(size=2)

    return(ggplotly(r, tooltip = **c("x","y", "series" )**) %>% plotly::config(displayModeBar = T)  %>%
             layout(legend = list(orientation = "h", x = 0.4, y = -0.2)))

And the result is:

新结果,有点好

How can I change the tooltip? Can I add words? I tried with paste("Dates","x") but doesn't work.

Thanks for your help.

You can use text in style to change the hover text.

The plotly object will have values accessible through a list as below. The date values will need to be converted with as.Date .

Edit : The code includes a full shiny app as a demo.

library(xts)
library(shiny)

data<-rnorm(10)
dates <- seq(as.Date("2016-01-01"), length =10, by = "days")
xtsMyData <- xts(x = data, order.by = dates)

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

server <- function(input, output, session) {
  output$myplot <- renderPlotly({
    r <- ggplot(tidy(xtsMyData), aes(x=index,y=value, color=series, type = 'scatter', mode = 'lines')) + 
      geom_line(size=2)

    r <- ggplotly(r) %>% 
      plotly::config(displayModeBar = T) %>%
      layout(legend = list(orientation = "h", x = 0.4, y = -0.2)) 

    r %>%
      style(text = paste0("Date:", as.Date(r$x$data[[1]]$x), 
                          "</br></br>", 
                          "Cantidad:", r$x$data[[1]]$y))
  })
}

shinyApp(ui, server)

Plot

修改悬停文本的绘图

The first answer gave me the idea to change manually all, because I had 2 different geom_lines and that didn't work for me, this labels are stored in r$x$data[[1]]$text (the following line plots are in r$x$data[[2]]$text,r$x$data[[3]]$text ... ), so, if you use an gsub, you could change everything you want, it's very dumb but it works. (You can use the same philosophy to delete the last label, manipulating strings)

I put an example for your problem, despite you already solve it, other person could have more than one line plot.

r$x$data[[1]]$text<-gsub(r$x$data[[1]]$text,pattern='index', replacement='Fecha')

r$x$data[[1]]$text<-gsub(r$x$data[[1]]$text,pattern='value', replacement='Valor')

r$x$data[[1]]$text<-gsub(r$x$data[[1]]$text,pattern='series', replacement='Serie')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM