Multiple y-axes chart with Plotly in R
<p>In Plotly for Python, we have this beautiful multiple y-axis example:
<a href="https://i.stack.imgur.com/1bXCI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1bXCI.png" alt="enter image description here"></a>
here is the <a href="https://plot.ly/python/multiple-axes/" rel="noreferrer">link</a> for the code.</p>

<p>I tried to do the same with Plotly in R using this code:</p>

<pre><code>  library(plotly)

x &lt;- 1:4
y1 &lt;- c(1,2,3,4)
y2 &lt;- c(4,3,2,1)
y3 &lt;- c(1,4,1,4)
y4 &lt;- c(4,1,4,1)

test &lt;- data.frame(x, y1, y2, y3, y4)

plot_ly(data = test, x = ~x, y = ~y1
        ,type = "scatter", mode = "lines", width = 800, color = I("red")
        ,name = "name01") %&gt;%
  add_trace(x = ~x, y = ~y2, yaxis = "y2", color = I("blue"), name = "name02") %&gt;%
  add_trace(x = ~x, y = ~y3, yaxis = "y3", color = I("purple"), name = "name03") %&gt;%
  add_trace(x = ~x, y = ~y4, yaxis = "y4", color = I("green"), name = "name04") %&gt;%
  layout(
    yaxis = list(
      showline = FALSE, side = "left"
      ,title = "Label 1"
      ,color = "red"
    )
    ,yaxis2 = list(
      showline = FALSE
      ,overlaying = "y"
      ,title = "Label 2", anchor = "free"
      ,color = "blue"
    )
    ,yaxis3 = list(
      showline = FALSE, side = "right", overlaying = "y"
      ,title = "Label 3"
      ,color = "purple"
    )
    ,yaxis4 = list(
      showline = FALSE, side = "right", overlaying = "y", position = 1
      ,title = "Label 4", anchor = "free"
      ,color = "green"
    )
    ,xaxis = list(
      showline = FALSE, zeroline = FALSE, dtick = 1, title = ""
    )
    ,showlegend = FALSE
    ,margin = list(
      pad = 30, b = 90, l = 150, r = 90
    )
    ,legend = list(orientation = "h")
  )
</code></pre>

<p>But I get these overlapping labels:</p>

<p><a href="https://i.stack.imgur.com/DpqKc.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DpqKc.png" alt="enter image description here"></a> </p>

<p>How can I fix it to get a non overlapping chart?</p>