首页 > 解决方案 > 在 plotly 图表中添加水平滚动条

问题描述

如何在长线图中添加水平 x 轴滚动条?

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

rangeslider()不起作用的情况。

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)

标签: rplotlyr-plotly

解决方案


我建议使用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

结果


@firmo23 编辑后:

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
  )

结果


推荐阅读