Home > Software design >  Is there a way to plot line with start and end coordinate?
Is there a way to plot line with start and end coordinate?

Time:03-11

I have this table and I would like to plot the start and end coordinate of petals and leaves (one color for petls and another for leaves).

string id start end
petals id 50 100
leaves id 101 151
petals id 152 202
leaves id 203 253
petals id 254 305
leaves id 306 356

I don't want represent it with a dot but with a line starting from the start coordinate and ending at the end coordinate for petals and leaves represented with different color.

Is there any way?

CodePudding user response:

I guess you could have something like this:

library(ggplot2)

ggplot(df, aes(y = string, x = start, colour = string))  
  geom_segment(aes(yend = string, xend = end), size = 3)  
  scale_color_manual(values = c("yellowgreen", "violet"))  
  theme_bw()  
  coord_fixed(20)  
  labs(x = "Position")

enter image description here

If you want the lines at the same height you could do:

ggplot(df, aes(y = 1, x = start, colour = string))  
  geom_segment(aes(yend = 1, xend = end), size = 3)  
  scale_color_manual(values = c("yellowgreen", "violet"))  
  theme_bw()  
  coord_fixed(500)  
  labs(x = "Position")  
  theme(axis.title.y = element_blank(),
        axis.ticks.length.y = unit(0, "mm"),
        axis.text.y = element_blank())

enter image description here


Data

df <- structure(list(string = c("petals", "leaves", "petals", "leaves", 
"petals", "leaves"), id = c("id", "id", "id", "id", "id", "id"
), start = c(50L, 101L, 152L, 203L, 254L, 306L), end = c(100L, 
151L, 202L, 253L, 305L, 356L)), class = "data.frame", row.names = c(NA, 
-6L))
  • Related