I am programming a Shiny app in R which makes use of the dygraphs package.
The app features an interactive plot, which highlights the selected series using the dyHighlight()
function. However, since the data involves 1000 columns, the legend consequently displays 1000 series.
Here is a minimal code example with only 2 columns:
ui <- fluidPage(
# This CSS code should solve the problem, but somehow does not.
# To test, replace CSS in tags$style(...) with this code
#.dygraph-legend { display: none; }
#.dygraph-legend .highlight { display: inline; }
# CSS to highlight selected series in legend.
# Does not solve problem, but is best alternative for now.
tags$style("
.highlight {
border: 2px solid black;
background-color: #B0B0B0;
}
"),
sidebarLayout(
sidebarPanel(),
mainPanel(dygraphOutput("plot"))
)
)
server <- function(input, output) {
set.seed(123)
data <- matrix(rnorm(12), ncol = 2)
data <- ts(data)
# Workaround for what might be a bug
# Reference: https://stackoverflow.com/questions/28305610/use-dygraph-for-r-to-plot-xts-time-series-by-year-only
data <- cbind(as.xts(data[,1]), as.xts(data[,2]))
colnames(data) <- c("Series 1", "Series 2")
#print(data) # Uncomment to view data frame
output$plot <- renderDygraph({
dygraph(data) %>%
# Highlighting series
dyHighlight(data,
highlightCircleSize = 2,
highlightSeriesBackgroundAlpha = .5,
highlightSeriesOpts = list(strokeWidth = 2))
})
}
shinyApp(ui = ui, server = server)
Is there a way to adjust the legend in R/Shiny so that only the highlighted series (and not all series at the same time point) is shown?
The lower 2 plots in the following link show exactly what should be achieved: http://dygraphs.com/gallery/#g/highlighted-series
This has already been questioned several times on stackoverflow, but has not been answered yet or does not work (+ I do not want to necro posts):
Is there a way to highlight closest series in R dygraphs?
Highlight Closest Series - but only show X/Y of highlighted series?
100 labels in separate div with highlighted series in a box
Anyhow, dyHighlight()
does not seem to have a built-in argument that supports this feature, so JavaScript and CSS are probably needed.
Searching the internet led me to highlightSeriesOpts
, highlightCallback
and unhighlightCallback
from the following link:
http://dygraphs.com/options.html
But how do you use these options in R?