chippycentra
chippycentra

Reputation: 3432

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.

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))

Upvotes: 2

Views: 97

Answers (2)

Rfanatic
Rfanatic

Reputation: 2280

Colored by sequences;

Sample code:

library(ggplot2)

ggplot(df, aes(y = seq, x = start, colour = seq)) +
  geom_segment(aes(yend = seq, xend = end), size = 3) +
  labs(x = "Position", y="", color="Sequences")+
  theme_minimal()

Plot:

enter image description here

Sample data:

df<-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))

Upvotes: 1

Waldi
Waldi

Reputation: 41210

With geom_segment:

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

enter image description here

Upvotes: 2

Related Questions