添加第二个轴以绘图而不同时添加跟踪 [英] Adding second axis to plotly plot without simultaneously adding trace

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

问题描述

由于 ggplotly 不支持 ggplot2sec.axis (

解决方案

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

图书馆(情节)是 <- 列表(滴答字体=列表(颜色=红色"),覆盖=y",边=对",title = "第二个 y 轴")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) %>%布局(title = "双 Y 轴", yaxis2 = ay,xaxis = list(title="x"))p

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.

Example:

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

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

解决方案

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天全站免登陆