Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R make interactive needle plot using plotly package

I want to make an interactive needle plot using R package plotly similar as described here

By using ggplot2 and plotly, I could easily make an interactive needle plot:

library(ggplot2)
library(plotly)
p <- ggplot(data.frame(x=1:10, y=1:10),
       aes(x=x, ymax=y, ymin=0)) +
       geom_linerange()
ggplotly(p)

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However, when the data size is too big, the above code to generate the needle plot become very slow.

I am wondering if I could make the needle plot directly using ggplotly.

Thanks a lot for your help.

>Solution :

You could use add_segments with specifying y = 0 like this:

library(dplyr)
library(plotly)
data.frame(x=1:10, y=1:10) %>%
  plot_ly(data = .) %>%
  add_segments(x=~x, xend = ~x, y = 0, yend=~y)

Created on 2023-04-05 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading