【问题标题】:Adding second axis to plotly plot without simultaneously adding trace添加第二个轴以绘图而不同时添加跟踪
【发布时间】:2020-01-08 01:25:48
【问题描述】:

由于ggplotly 不支持ggplot2sec.axis (Adding second Y axis on ggplotly),所以我想向情节对象添加第二个轴。但是,我不想添加任何新的痕迹。

示例:

library(plotly)
ay <- list(
  tickfont = list(color = "red"),
  overlaying = "y",
  side = "right",
  title = "second y axis"
)
p <- plot_ly() %>%
  add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
  add_lines(x = ~2:4, y = ~1:3, name = "slope of 1", yaxis = "y2") %>%
  layout(
    title = "Double Y Axis", yaxis2 = ay,
    xaxis = list(title="x")
  )

p

如何在没有add_lines 或添加任何其他跟踪的情况下完成显示yaxis = "y2"

【问题讨论】:

    标签: r ggplot2 plotly


    【解决方案1】:

    实现此目的的一种方法是执行您已完成的操作,并将您为第二个轴添加的任何颜色更改为“透明”,然后关闭该行的 hoverinfo 和图例条目:

    library(plotly)
    ay <- list(
      tickfont = list(color = "red"),
      overlaying = "y",
      side = "right",
      title = "second y axis"
    )
    p <- plot_ly() %>%
      add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
      add_lines(x = ~2:4, y = ~1:3, color = I("transparent"), name = "", yaxis = "y2", hoverinfo='skip', showlegend=FALSE) %>%
      layout(
        title = "Double Y Axis", yaxis2 = ay,
        xaxis = list(title="x")
      )
    p
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 2019-03-20
      相关资源
      最近更新 更多