Search code examples
rplotlyr-plotly

Add horizontal scroll bar in plotly chart


How can I add a horizontal x-axis scroll bar in a long plotly line chart?

library(plotly)

x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)

fig <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')

fig

Case that rangeslider() does not work.

VaccinationWeek<-c("2020w1","2020w1","2020w1","2020w2","2020w2","2020w2")
Country<-c("EU","CHE","ITA","EU","CHE","ITA")
Value<-c(3,2,1,5,3,2)
dat<-data.frame(VaccinationWeek,Country,Value)
plot_ly(dat,
        x = ~VaccinationWeek, 
        y = ~Value/100,
        text = ~Value,
        color = ~Country,
        customdata = dat$Country) %>%
  add_trace(
    type = 'scatter',
    mode = 'lines+markers',
    hovertemplate = paste("Country: %{customdata}",
                          "Uptake full vaccination (%): %{y}", 
                          "<extra></extra>",
                          sep = "\n"),
    hoveron = 'points') %>%
  add_text(    
    textposition = "top center",
    showlegend = F,
    hoverinfo = "skip") %>% 
  layout(font = list(color = '#a2a2a2'),title=list(text="by reporting week",x = 0),
         xaxis = list(fixedrange = TRUE,title="",showgrid = FALSE,tickangle = 45
         ),
         yaxis = list(fixedrange = TRUE,rangeslider = list(),title="",showgrid = FALSE,showline=T,tickformat = "%"),
         hovermode = "x unified",
         hoverlabel = "none",
         legend = list(itemclick = F, itemdoubleclick = F))%>%
  config(modeBarButtonsToRemove = c('toImage',"zoom2d","toggleSpikelines","hoverClosestCartesian","hoverCompareCartesian","drawline","autoScale2d" ,"resetScale2d","zoomIn2d","zoomOut2d","pan2d",'select2d','lasso2d'))%>%
  config(displaylogo = FALSE)

Solution

  • I'd suggest using a rangeslider:

    library(plotly)
    
    x <- c(1:100)
    random_y <- rnorm(100, mean = 0)
    data <- data.frame(x, random_y)
    
    fig <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines') %>%
      layout(xaxis = list(rangeslider = list()))
    
    fig
    

    result


    After @firmo23's edit:

    library(plotly)
    
    VaccinationWeek <- c("2020w1", "2020w1", "2020w1", "2020w2", "2020w2", "2020w2")
    Country <- c("EU", "CHE", "ITA", "EU", "CHE", "ITA")
    Value <- c(3, 2, 1, 5, 3, 2)
    dat <- data.frame(VaccinationWeek, Country, Value)
    plot_ly(
      dat, x = ~ VaccinationWeek, y = ~ Value / 100, text = ~ Value, color = ~ Country, customdata = dat$Country
    ) %>%
      add_trace(
        type = 'scatter', mode = 'lines+markers', hovertemplate = paste(
          "Country: %{customdata}", "Uptake full vaccination (%): %{y}", "<extra></extra>", sep = "\n"
        ), hoveron = 'points'
      ) %>%
      add_text(textposition = "top center", showlegend = F, hoverinfo = "skip") %>%
      layout(
        font = list(color = '#a2a2a2'), title = list(text = "by reporting week", x = 0), xaxis = list(
          fixedrange = TRUE, title = "", showgrid = FALSE, tickangle = 45, rangeslider = list()
        ), yaxis = list(
          fixedrange = TRUE, rangeslider = list(), title = "", showgrid = FALSE, showline = T, tickformat = "%"
        ), hovermode = "x unified", hoverlabel = "none", legend = list(itemclick = F, itemdoubleclick = F)
      ) %>%
      config(
        modeBarButtonsToRemove = c(
          'toImage',
          "zoom2d",
          "toggleSpikelines",
          "hoverClosestCartesian",
          "hoverCompareCartesian",
          "drawline",
          "autoScale2d" ,
          "resetScale2d",
          "zoomIn2d",
          "zoomOut2d",
          "pan2d",
          'select2d',
          'lasso2d'
        ),
        displaylogo = FALSE
      )
    

    result