在ggplot2中的geom_segment旁边添加文本

问题描述

你好,我有一个df,例如:

标签

  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

到目前为止,此代码

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,label = gene),nudge_y = 0.5,size=2) + 
  scale_y_discrete(limits = rev(levels(tab$molecule))) +
  theme_minimal()

我设法得到了这个情节:

enter image description here

,我想知道是否有一种方法可以在geom_segment旁边添加带有COL1COL2值的文本并根据阈值为文本着色:绿色值> 10,红色值

得到类似的东西

image

dput(标签

structure(list(X = c(7L,5L,4L,2L,6L,3L,1L),molecule = structure(c(1L,4L),.Label = c("scaffold_1254","scaffold_15158","scaffold_7180","scaffold_74038","scaffold_7638","scaffold_8315"
),class = "factor"),gene = structure(c(7L,.Label = c("G1","G2","G3","G4","G5","G6","G7"),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
    ),end_scaff = c(20072L,15336L,13487L,10827L,10155L,strand = structure(c(1L,2L),.Label = c("backward","forward"),direction = c(-1L,-1L,COL1 = c(10L,18L,25L,21L,8L),COL2 = c(20L,9L,7L,34L)),class = "data.frame",row.names = c(NA,-7L))

解决方法

一个近似值是

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,label = gene),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