如何在 plotly 中绘制 y 轴刻度之间的水平线?

How to plot horizontal lines between y-axis ticks in plotly?

下面是一些示例代码来说明我的问题。

library(plotly)

p <- plot_ly(x = mtcars$mpg, y = seq_along(rownames(mtcars)), text=rownames(mtcars),
             type = 'scatter', mode = 'markers')


ax <- list(
  title = "",
  ticktext = rownames(mtcars),
  tickvals = seq(1,32)
)


line <- list(
  type = "line",
  line = list(color = "pink"),
  xref = "x",
  yref = "y"
  layer = 'below'
)

lines <- list()
for (i in seq_along(rownames(mtcars))) {
  line[["x0"]] <- mtcars$mpg[i] - 1
  line[["x1"]] <- mtcars$mpg[i] + 1
  line[c("y0", "y1")] <- i
  lines <- c(lines, list(line))
}

p <- layout(p, title = 'Highlighting with Lines', shapes = lines, yaxis=ax)
p

我想在图中添加水平线以分隔每个 y 轴标签。我更喜欢这条线分割标签和图表,但只分割图表就足够了。我已经广泛查看了 layout 部分的 y-axis 中的 plotly reference but have yet to find anything that appears to help. I was told that there might be some sort of solution through some ,但我不确定我将如何处理这个/我不是很精通 JS。

我能够通过向 lines 列表添加更多行来完成此操作。在上图的for循环下,如果加入代码:

for (i in seq_along(rownames(mtcars))) {
  line[["x0"]] <- 10 
  line[["x1"]] <- 35
  line[c("y0", "y1")] <- i-.5
  lines <- c(lines, list(line))
}

它将在每条数据线之间放置一条分隔线。