thackl / gggenomes

A grammar of graphics for comparative genomics
https://thackl.github.io/gggenomes/
Other
579 stars 64 forks source link

Overlapping text lables #130

Closed minalj365 closed 1 year ago

minalj365 commented 1 year ago

Hi there! Is there any way like geom_text_repel for overlapping labels? Here is the plot where I have overlapping labels:

image

And this is the code:


gggenome +
  geom_seq(size=1) +         # draw contig/chromosome lines
  geom_bin_label(size = 4) +   # label each sequence 
  geom_gene(aes(fill=gene), size=3) +        # draw genes as arrow
  geom_gene_tag(aes(label=gene), nudge_y=0.2, check_overlap = FALSE, hjust = 0.1) +
  theme(axis.text.x = element_text(size=12), legend.position = "none")
thackl commented 1 year ago

I haven't directly implemented that in gggenomes, but you can use ggrepel to do it:

library(gggenomes)

s0 <- tibble(
  seq_id = "A",
  length = 5000
)

g0 <- tibble(
  seq_id = "A",
  start=c(1000, 1500, 1550),
  end=start+50,
  gene=c("fooo", "baaar", "baaaz")
)

gggenomes(g0, s0) +
  geom_seq() + geom_gene() +
  geom_gene_tag(aes(label=gene))

image

library(ggrepel)
gggenomes(g0, s0) +
  geom_seq() + geom_gene() +
  geom_text(aes(x=(x+xend)/2, y=y+.1, label=gene, color="overlapping"), data=genes(),
                  hjust=0, vjust=0, angle=45) +
  geom_text_repel(aes(x=(x+xend)/2, y=y+.2, label=gene, color="repelled"), data=genes(),
                  hjust=0, vjust=0, angle=45, direction = "x")

image

Check out https://ggrepel.slowkow.com/articles/examples.html#align-labels-on-the-top-or-bottom-edge-1 for additional ideas to draw lines between genes and tags, etc...

minalj365 commented 1 year ago

Thank you so much! Really helpful!