【问题标题】:show right y axis in plotly chart with only 1 trace在绘图图中显示右 y 轴,只有 1 条轨迹
【发布时间】:2021-01-31 09:28:23
【问题描述】:

在 R plotly 中,我想显示一个在左侧和右侧都有 y 轴标签的单线图。我了解how to do this with 2 or more traces,但我找不到任何地方显示如何在图表上仅使用 1 条迹线进行操作。这是一个基本示例 - 它仅在左侧显示 y 轴,但我希望它出现在两侧:

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

fig

【问题讨论】:

标签: r plotly r-plotly


【解决方案1】:

您可以像这样在新轴中添加相同的值:

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

输出:

如果要删除图例:

#Plot 2
fig <- plot_ly()
fig <- fig %>% add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10", yaxis = "y2")
fig <- fig %>% 
  add_lines(x = ~1:3, y = ~10*(1:3), name = "slope of 10") %>%
  layout(
  title = "Double Y Axis", yaxis2 = ay,
  xaxis = list(title="x"),showlegend = FALSE
)

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-13
    • 2012-06-05
    • 2017-01-18
    • 2017-09-19
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多