1

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

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 轴标签。我更喜欢分割标签和图表的线,但只分割图表就足够了。我已经通过plotly参考进行了广泛的查看,但还没有找到任何似乎有帮助的东西。有人告诉我,可能会通过该部分中的一些自定义 JS找到某种解决方案,但我不确定我将如何解决这个问题/我不是很懂 JS。y-axislayout

4

1 回答 1

0

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

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

于 2017-07-13T14:22:29.047 回答