Black Lives Matter. Please consider donating to Black Girls Code today.
Dash Enterprise delivers an incredible 21x cost savings 💸Download the e-book!

Extend geom_hline segment in ggplotly indefinitely

I would like to make a plot that has horizontal line (not at the x-axis), but I would like this line to extend indefinitely instead of just being a line segment. So far, I’m not sure how I can do this with plot_ly or ggplotly.

Basically, I want the person viewing the plot to be able to zoom out and always see the line extend to the edges of the graph.

Here is a toy example:
p <- ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()
p <- p + geom_hline(yintercept = 20, size = .25)

With the recent changes to the plotly package, this should work fine :thumbsup:

Hmm… I updated to the latest version via github and it still didn’t make the line extend indefinitely.

That’s odd. With v 3.0.1 it works fine (for me anyway). Which version are you using? Also, can you share a screenshot of you get when you run that code snippet?

I upgraded to version 3.1.0 of plotly and it still doesn’t do it for me. Here is a picture.

This is odd. Maybe share your sessionInfo() ?Also, which version of ggplot2 are you using?

I am having the same issue. Here is my sessionInfo() output. I appreciate any help. Thanks again for your contribution.

R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)

[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] stringr_1.0.0 stringi_0.5-5 dplyr_0.4.3 RSQLServer_0.1.1 rJava_0.9-7 DBI_0.3.1
[7] RODBC_1.3-12 shinydashboard_0.5.1 shiny_0.12.1 plotly_3.4.1 ggplot2_2.1.0

loaded via a namespace (and not attached):
[1] assertthat_0.1 base64enc_0.1-3 colorspace_1.2-6 digest_0.6.9 grid_3.1.2 gridExtra_2.0.0 gtable_0.1.2 htmltools_0.3
[9] htmlwidgets_0.5 httpuv_1.3.2 httr_1.1.0 jsonlite_0.9.19 labeling_0.3 magrittr_1.5 mime_0.4 munsell_0.4.3
[17] parallel_3.1.2 plyr_1.8.3 R6_2.1.2 Rcpp_0.12.3 RJDBC_0.2-5 scales_0.3.0 tidyr_0.4.1 tools_3.1.2
[25] viridis_0.3.2 xtable_1.7-4 yaml_2.1.13