用多轴绘制图

时间:2017-08-17 08:29:58

标签: r plot plotly

我在R中创建了一个在同一侧有多个y轴的图。然而,附加轴覆盖了绘图,当图形更嘈杂时会导致问题。

这就是我所拥有的:

enter image description here

虽然我需要的是这样的东西:

enter image description here

示例代码:

library(plotly)
ay <- list(
  tickfont = list(color = "red"),
  overlaying = "y",
  side = "left",
  title = "second y axis",
  position = 0.1
)
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

1 个答案:

答案 0 :(得分:1)

据我所知,在plotly中没有为R正确管理多个y轴,唯一可以解决上述问题的技巧是调整margin属性,如建议的here

library(plotly)
ay <- list(
  tickfont = list(color = "red"),
  overlaying = "y",
  side = "left",
  title = "second y axis",
  anchor="free"
)

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"), 
    yaxis = list(showline = FALSE, side="left"),
    margin=list(pad = 50, b = 90, l = 150, r = 90)
  )   
p

enter image description here