简体   繁体   English

R / Shiny字签包中的系列突出显示图例

[英]Series highlighting legend in dygraphs package for R/Shiny

I am programming a Shiny app in R which makes use of the dygraphs package. 我正在R中编写一个使用dygraphs包的Shiny应用程序。

The app features an interactive plot, which highlights the selected series using the dyHighlight() function. 该应用程序具有交互式绘图,可使用dyHighlight()函数突出显示所选系列。 However, since the data involves 1000 columns, the legend consequently displays 1000 series. 但是,由于数据涉及1000列,因此图例显示1000个系列。

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? 有没有一种方法可以在R / Shiny中调整图例,以便仅显示突出显示的系列(而不是同时显示所有系列)?
The lower 2 plots in the following link show exactly what should be achieved: http://dygraphs.com/gallery/#g/highlighted-series 以下链接中的下2个图准确显示了应实现的目标: 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): 这已经在stackoverflow上被问过好几次了,但是还没有得到回答或不起作用(+我不想破坏帖子):
Is there a way to highlight closest series in R dygraphs? 有没有办法突出显示R字图中最接近的系列?
Highlight Closest Series - but only show X/Y of highlighted series? 高亮显示最近的系列-但仅显示高亮显示的系列的X / Y?
100 labels in separate div with highlighted series in a box 单独的div中有100个标签,并且在框中突出显示了系列

Anyhow, dyHighlight() does not seem to have a built-in argument that supports this feature, so JavaScript and CSS are probably needed. 无论如何, dyHighlight()似乎没有支持该功能的内置参数,因此可能需要JavaScript和CSS。

Searching the internet led me to highlightSeriesOpts , highlightCallback and unhighlightCallback from the following link: http://dygraphs.com/options.html 搜索互联网使我highlightSeriesOptshighlightCallbackunhighlightCallback从以下链接: http://dygraphs.com/options.html

But how do you use these options in R? 但是如何在R中使用这些选项?

This should get you started: 这应该使您开始:

https://rstudio.github.io/dygraphs/gallery-css-styling.html https://rstudio.github.io/dygraphs/gallery-css-styling.html

http://dygraphs.com/css.html http://dygraphs.com/css.html

The legend has the .dygraph-legend class. 图例具有.dygraph-legend类。 When using highlightSeriesOpts, the selected series' gets a .highlight class. 使用HighlightSeriesOpts时,所选系列会获得一个.highlight类。 This can be used to show only a single series in the legend. 这只能用于显示图例中的单个系列。

So you have to create CSS file and add it to the grapgh: https://rstudio.github.io/dygraphs/gallery-css-styling.html 因此,您必须创建CSS文件并将其添加到graggh中: https ://rstudio.github.io/dygraphs/gallery-css-styling.html

This should work, but for some reason .dygraph-legend is taking over and no legend is displayed. 这应该可以,但是由于某种原因, .dygraph-legend将接管,并且不显示图例。

 .dygraph-legend  { display: none; }

.highlight {
  display: inline;
}

Altenative: Altenative:

.dygraph-legend  { display: all; }

.highlight {
  display: inline;
  background-color: #B0B0B0;
}

Save it as *.css file: 将其另存为* .css文件:

dygraph(data)%>% dyHighlight() %>% dyCSS("path to css")

This does't solve the problem but adds a highlight to selected series: 这不能解决问题,但会为所选系列添加一个亮点: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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