title: R Plotly - Add annotation to reference line in a scatter plot
body: <p>I have created a scatterplot with Plotly in R using the iris dataset and have added two reference lines, one on the x-axis denoting the mean Sepal.Width and the other on the y-axis denoting the mean Sepal.Length.</p>
  <p>Below is the executable R code:</p>
  <pre><code>library(dplyr)
  library(plotly) 
  
  iris &lt;- datasets::iris
      
  scatterplot &lt;- plot_ly(data = iris, x = ~Sepal.Width, y = ~Sepal.Length, type = 'scatter',
                         text = ~Species,
                         color = I('orange')) %&gt;%
                 layout(shapes=list(list(type = 'line', 
                                       x0 = mean(iris$Sepal.Width), 
                                       x1 = mean(iris$Sepal.Width),
                                       y0 = 4, 
                                       y1 = 8, 
                                       line = list(width = 2)),
                                  list(type = 'line', 
                                       x0 = 1.5, 
                                       x1 = 5, 
                                       y0 = mean(iris$Sepal.Length), 
                                       y1 = mean(iris$Sepal.Length), 
                                       line = list(width = 2))))
      
  scatterplot
  </code></pre>
  <p>The above R code produces the following
  <a href="https://i.stack.imgur.com/HnjZ9.png" rel="nofollow noreferrer">Plotly output</a></p>
  <p>I want to add annotation text (i.e. label) for the two reference lines (&quot;Mean Sepal Width&quot; and &quot;Mean Sepal Length&quot;).</p>
  <p>I came across a <a href="https://stackoverflow.com/questions/41267246/how-to-add-annotations-horizontal-or-vertical-reference-line-in-plotly">similar post</a> however the solution mentioned there did not work for me. If someone can provide me the solution with the code then that would be highly appreciated.</p>
  <p>PS: I am using Plotly version 4.8.0</p>