我正在使用dygraphs軟件包編程R中的Shiny應用程序。R/Shiny dygraphs軟件包中突出顯示的圖例
該應用程序提供了一個交互式圖表,該圖表使用dyHighlight()
函數突出顯示所選系列。但是,由於數據涉及1000列,因此圖例顯示1000個系列。
這裏是僅2列的最小代碼例如:
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)
有一種方法來調整R中的圖例/閃亮,以便只突出顯示系列(而不是所有系列在同一時間點)顯示?
低2個地塊下面的鏈接節目究竟應該怎樣實現:http://dygraphs.com/gallery/#g/highlighted-series
這已經質疑計算器幾次,但尚未答覆或不工作(+我不想死靈帖子):
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
總之,dyHighlight()
似乎並不具備支持此功能,所以JavaScript和CSS很可能需要一個內置的說法。
搜索互聯網使我highlightSeriesOpts
,從以下鏈接highlightCallback
和unhighlightCallback
: http://dygraphs.com/options.html
但你如何使用R這些選項?
您在與重複的例子,這個問題的嘗試將是有益的 –
你應該有一個時間序列對象,你數據不是,請提供相關數據 –
完成,請參閱OP :) –