r - 在绘图中添加水平滚动条

标签 r plotly r-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)

最佳答案

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


@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
  )

result

关于r - 在绘图中添加水平滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67289361/

相关文章:

r - 需要通过多个公共(public)列合并多个数据框

python - Plotly.js 连接到 Django Rest Framework API - 这可能吗?如果可能的话,如何实现?

r - Plotly R 的 slider

r - 如何绘制多列 CSV 文件?

R:有条件删除矩阵中的行和列

python - Plotly Express 散点图不会绘制 x 变量的完整范围

r - R 中函数plot_ly 的相同代码在项目和 RGUI 中给出不同的结果 : Error in Layout, 未使用的参数

r - 在 plotly::subplot 中显示所有 plotly 标题

r - 禁用图例双击事件

r - ggplot2 : printing multiple plots in one page with a loop