Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
How to a hide single line in ggplotly grouped time series graph by default? (plotly,R,Javascript)

Plotly has this nice function to hide a plot, if you click on its name in the legend. The legend items turns grey and the line only appears again, if we click on the legend item again.

Consider you want to deploy a shiny app, where the user can look at the time series and additionally he can look at the series trend seasonality etc. in the same plot.

I want initially only the series to be displayed in the rendered ggplotly object, when the user opens the app. All other diagnostic plots like Trend (in my reprex) and seasonality should be hidden by default. Only when I click on the legend item Trend (reprex), the trend line should additionally appear.

I know how to do this in shiny, with shiny functions, but I want to be able to solve it with plotly or javascript functionality in the way I mentioned above.

reprex code (try to hide the line "Trend" by default):

library(plotly)
library(lubridate)
library(ggplot2)
set.seed(42)
data <- tibble(id = c(rep("A",24)),
               value = c(cumsum(rnorm(12)), seq( 1, 7, length.out = 12)),
               date = rep(c(ymd("2013-01-01")+ months(0:11)),2),
               label = c(rep("Series",12),rep("Trend", 12))
)
p <- ggplot(data, mapping = aes(x= date, y= value, color = label)) +
            geom_line()  +
            xlab("")
ggplotly(p) %>% layout(legend=list(y = -0.2,
                                   xanchor = 'left',
                                   yanchor = 'bottom',
                                   orientation = 'h')) 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use a style with traces which will be able to hide the Trend line only. You can use the following code:

library(plotly)
library(lubridate)
library(ggplot2)
set.seed(42)
data <- tibble(id = c(rep("A",24)),
               value = c(cumsum(rnorm(12)), seq( 1, 7, length.out = 12)),
               date = rep(c(ymd("2013-01-01")+ months(0:11)),2),
               label = c(rep("Series",12),rep("Trend", 12))
)
p <- ggplot(data, mapping = aes(x= date, y= value, color = label)) +
  geom_line()  +
  xlab("")

p <- ggplotly(p) %>% layout(legend=list(y = -0.2,
                                   xanchor = 'left',
                                   yanchor = 'bottom',
                                   orientation = 'h')) 

style(ggplotly(p),visible="legendonly", traces = 2)

Output:

enter image description here

As you can see in the image, only series is currently displayed and Trend is hided.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda