I would like to create a barplot with a horizontal line in plotly R. There is an option to use add_segments like here. Unfortunately it doesn't display the full width of the graph and cuts of at the centers of the bars. Here is a simple reproducible example:
library(plotly)
library(dplyr)
iris %>%
group_by(Species) %>%
summarise(sepal_length_tot = sum(Sepal.Length)) %>%
plot_ly() %>%
add_trace(mode = "bars", x = ~Species, y = ~sepal_length_tot, color = "Species") %>%
add_segments(x = "setosa", xend = "virginica", y = 200, yend = 200, color = "line")

Created on 2025-08-22 with reprex v2.1.0
As you can see it is not using the full width of the graph. So I was wondering how we can add a horizontal line to a barplot in plotly R and display it in the legend?


