chippycentra
chippycentra

Reputation: 3432

Add text next to geom_segment in ggplot2

Hello I have a df such as :

tab

  X       molecule gene start_gene end_gene start_scaff end_scaff   strand direction COL1 COL2
1 7  scaffold_1254   G7       6708    11967           1     20072 backward        -1   10   20
2 5  scaffold_7638   G5       9567    10665           1     15336 backward        -1   18    1
3 4  scaffold_7638   G4       3456     4479           1     15336  forward         1   18    1
4 2 scaffold_15158   G2      10105    10609           1     13487 backward        -1    5    9
5 6  scaffold_8315   G6       2760     3849           1     10827  forward         1   25    7
6 3  scaffold_7180   G3       9814    10132           1     10155 backward        -1   21    9
7 1 scaffold_74038   G1       1476     2010           1      2010  forward         1    8   34

so far with this code :

ggplot(tab, aes(x = start_scaff, xend = end_scaff, 
                y = molecule, yend = molecule)) +
  geom_segment(size = 3, col = "grey80") +
  geom_segment(aes(x = ifelse(direction == 1, start_gene, end_gene),
                   xend = ifelse(direction == 1, end_gene, start_gene)),
               data = tab, 
               arrow = arrow(length = unit(0.1, "inches")), size = 2) +
  geom_text_repel(aes(x = start_gene, y = molecule, label = gene),
            data = tab, nudge_y = 0.5,size=2) + 
  scale_y_discrete(limits = rev(levels(tab$molecule))) +
  theme_minimal()

I mannaged to get this plot : enter image description here

and I wondered if there were a way to add a text just next to geom_segment with COL1 and COL2 values and color the text depending on a threshold : green values > 10, red values <= 10

and get something like

image

dput(tab)

structure(list(X = c(7L, 5L, 4L, 2L, 6L, 3L, 1L), molecule = structure(c(1L, 
5L, 5L, 2L, 6L, 3L, 4L), .Label = c("scaffold_1254", "scaffold_15158", 
"scaffold_7180", "scaffold_74038", "scaffold_7638", "scaffold_8315"
), class = "factor"), gene = structure(c(7L, 5L, 4L, 2L, 6L, 
3L, 1L), .Label = c("G1", "G2", "G3", "G4", "G5", "G6", "G7"), class = "factor"), 
    start_gene = c(6708L, 9567L, 3456L, 10105L, 2760L, 9814L, 
    1476L), end_gene = c(11967L, 10665L, 4479L, 10609L, 3849L, 
    10132L, 2010L), start_scaff = c(1L, 1L, 1L, 1L, 1L, 1L, 1L
    ), end_scaff = c(20072L, 15336L, 15336L, 13487L, 10827L, 
    10155L, 2010L), strand = structure(c(1L, 1L, 2L, 1L, 2L, 
    1L, 2L), .Label = c("backward", "forward"), class = "factor"), 
    direction = c(-1L, -1L, 1L, -1L, 1L, -1L, 1L), COL1 = c(10L, 
    18L, 18L, 5L, 25L, 21L, 8L), COL2 = c(20L, 1L, 1L, 9L, 7L, 
    9L, 34L)), class = "data.frame", row.names = c(NA, -7L))

Upvotes: 1

Views: 1143

Answers (1)

iago
iago

Reputation: 3256

An approximation would be

ggplot(tab, aes(x = start_scaff, xend = end_scaff, 
                 y = molecule, yend = molecule)) +
     geom_segment(size = 3, col = "grey80") +
     geom_segment(aes(x = ifelse(direction == 1, start_gene, end_gene),
                      xend = ifelse(direction == 1, end_gene, start_gene)),
                  data = tab, 
                  arrow = arrow(length = unit(0.1, "inches")), size = 2) +
     geom_text_repel(aes(x = start_gene, y = molecule, label = gene),
                     data = tab, nudge_y = 0.5,size=2) + 
     scale_y_discrete(limits = rev(levels(tab$molecule))) +
     theme_minimal() + 
     geom_text(data = mutate(tab, COLr1 = COL1<10), aes(color = COLr1, label = COL1), position = position_nudge(x=20000)) + 
     geom_text(data = mutate(tab, COLr2 = COL2<10), aes(color = COLr2, label = COL2), position = position_nudge(x=22000)) +
     geom_text(data = mutate(tab, txt = "-"), aes(label = txt), position = position_nudge(x=21100)) +
     scale_color_manual(values = c("darkgreen", "red")) + 
     xlim(c(NA,23000)) + 
     theme(legend.position = "none")

enter image description here

Upvotes: 1

Related Questions