在不同时添加迹线的情况下添加第二个轴以进行绘图 [英] Adding second axis to plotly plot without simultaneously adding trace

查看:63
本文介绍了在不同时添加迹线的情况下添加第二个轴以进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于ggplotly不支持ggplot2sec.axis(在ggplotly上添加第二个Y轴) ,我想向绘图对象添加第二条轴.但是,我不想添加任何新的跟踪.

Since ggplotly does not supportggplot2's sec.axis (Adding second Y axis on ggplotly), I want to add a second axis to the plotly-object instead. However, I do not wish to add any new trace.

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

如何在不显示yaxis = "y2"的情况下显示yaxis = "y2"或添加任何其他跟踪记录?

How do I accomplish showing yaxis = "y2" without add_lines or adding any other trace?

推荐答案

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

One way to achieve this is to do what you have done and change the color of whatever you add for the second axis to "transparent", and turn off the hoverinfo and legend entry for the line :

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

这篇关于在不同时添加迹线的情况下添加第二个轴以进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆