【问题标题】:How to plot horizontal lines between y-axis ticks in plotly?如何在 plotly 中绘制 y 轴刻度之间的水平线?
【发布时间】:2017-12-16 01:27:45
【问题描述】:

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

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 reference 进行了广泛的查看,但还没有找到任何似乎有帮助的东西。有人告诉我,通过layout 部分的y-axis 中的一些custom JS 可能有某种解决方案,但我不确定我将如何解决这个问题/我不是很懂JS。

【问题讨论】:

    标签: r plotly horizontal-line


    【解决方案1】:

    我可以通过在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))
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多