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

Plot bar coordinates along a x axis with ggplot2

I have a dataframe such as

Seq start end 
S1  20    30
S2  25    35
S3  40    45
S4  41    60
S5  20    60
S6  10    30

And I would like to create with ggplot a figure such as :
enter image description here

Where I plot each bar coordinates within the X axis.

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

There is no preference about the order of the bars within the y axis, but they need to not overlap.

Here is the dput dataframe if it can helps

structure(list(Seq = c("S1", "S2", "S3", "S4", "S5", "S6"), start = c(20L, 
25L, 40L, 41L, 20L, 10L), end = c(30L, 35L, 45L, 60L, 60L, 30L
)), class = "data.frame", row.names = c(NA, -6L))

>Solution :

With geom_segment:

ggplot(data) + geom_segment(aes(x = start,
                                y = Seq,
                                xend = end,
                                yend = Seq),
                                size = 3)

enter image description here

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