R Shiny XTS - 使用 ggplot 更改默认工具提示的名称
R Shiny XTS - Change name of the default tooltip using ggplot
我有一个 xts object that has dates as values, I'm using ggplot2 and shiny 应用程序可以显示结果。
但是我想在鼠标在线时更改工具提示的默认名称。
来自:
index: 2020-03-19
value: 70
收件人:
Date: 2020-03-19
Cantidad: 70
XTS 代码:
data<-rnorm(10)
dates <- seq(as.Date("2016-01-01"), length =10, by = "days")
xtsMyData <- xts(x = data, order.by = dates)
剧情:
r <- ggplot(tidy(xtsMyData), aes(x=index,y=value, color=series, type = 'scatter', mode = 'lines')
) + geom_line(size=2)
结果是:
我正在尝试以下代码:
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)))
结果是:
如何更改工具提示?我可以添加单词吗? 我尝试使用 paste("Dates","x") 但没有用。
感谢您的帮助。
您可以在 style
中使用 text
来更改悬停文本。
plotly
对象将具有可通过以下列表访问的值。日期值需要使用 as.Date
.
进行转换
编辑:该代码包含一个完整的闪亮应用程序作为演示。
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)
情节
第一个答案给了我全部手动更改的想法,因为我有 2 个不同的 geom_lines 而这对我不起作用,此标签存储在 r$x$data[[1]]$text
中(下一行情节在 r$x$data[[2]]$text,r$x$data[[3]]$text
... ),所以,如果你使用 gsub,你可以改变你想要的一切,它非常愚蠢但它有效。 (你可以使用相同的原理来删除最后一个标签,操作字符串)
我为你的问题举了一个例子,尽管你已经解决了,其他人可能有不止一个线图。
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')
我有一个 xts object that has dates as values, I'm using ggplot2 and shiny 应用程序可以显示结果。 但是我想在鼠标在线时更改工具提示的默认名称。
来自:
index: 2020-03-19
value: 70
收件人:
Date: 2020-03-19
Cantidad: 70
XTS 代码:
data<-rnorm(10)
dates <- seq(as.Date("2016-01-01"), length =10, by = "days")
xtsMyData <- xts(x = data, order.by = dates)
剧情:
r <- ggplot(tidy(xtsMyData), aes(x=index,y=value, color=series, type = 'scatter', mode = 'lines')
) + geom_line(size=2)
结果是:
我正在尝试以下代码:
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)))
结果是:
如何更改工具提示?我可以添加单词吗? 我尝试使用 paste("Dates","x") 但没有用。
感谢您的帮助。
您可以在 style
中使用 text
来更改悬停文本。
plotly
对象将具有可通过以下列表访问的值。日期值需要使用 as.Date
.
编辑:该代码包含一个完整的闪亮应用程序作为演示。
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)
情节
第一个答案给了我全部手动更改的想法,因为我有 2 个不同的 geom_lines 而这对我不起作用,此标签存储在 r$x$data[[1]]$text
中(下一行情节在 r$x$data[[2]]$text,r$x$data[[3]]$text
... ),所以,如果你使用 gsub,你可以改变你想要的一切,它非常愚蠢但它有效。 (你可以使用相同的原理来删除最后一个标签,操作字符串)
我为你的问题举了一个例子,尽管你已经解决了,其他人可能有不止一个线图。
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')