thackl / gggenomes

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

Draw links as single lines? #129

Closed TC-Hewitt closed 1 year ago

TC-Hewitt commented 2 years ago

Hello, this is a great package. I'm plotting something like below (ignore the messy labels and colors): example which was generated something like so:

p1 <- gggenomes(genes = my_genes, seqs = my_seqs, feats = NULL, links = NULL)
p1 %>% add_clusters(my_clusts) +
geom_seq() + 
geom_link() + 
geom_gene(aes(fill=cluster_id))

The genes are automatically linked and colored by cluster as intended. However, is there a way to make the links appear as single connecting lines instead of adopting the thickness of the genes? For larger plots it gets quite messy. Ideally I just want a black, dashed line connecting each orthologous gene. Thank you

TC-Hewitt commented 2 years ago

I have figured out a hacky solution in the meantime using trace to temporarily modify the geom_link function by changing the line default_aes <- aes(y = y, x = x, xend = xend, yend = yend, xmin = xmin, xmax = xmax) to default_aes <- aes(y = y, x = x, xend = x, yend = yend, xmin = xmax, xmax = xmax) then specifying linetype, offset and color with the function itself geom_link(linetype=3, color="black", offset=0.1) seems ok but not ideal as the lines are not centered to the middle of the genes. Please let me know if there is a better alternative? Thank you

thackl commented 2 years ago

Hi,

interesting question. You can use base ggplot::geom_segment to draw just lines between genes:

library(gggenomes)

p1 <- gggenomes(emale_genes, emale_seqs) |> add_clusters(emale_cogs) +
  geom_seq() + geom_gene()

# classic polygon links
p1 + geom_link()

image

# line link with use standard ggplot geom_segment
# need to explicitly set aes() and data=links() here!
# x/xend: link start/end on seq1
# xmin/xmax: link start/end on seq2
p1 + geom_segment(aes(y=y, yend=yend, x=(x+xend)/2, xend=(xmin+xmax)/2), data=links())

image

Hope that helps! Thomas

thackl commented 2 years ago

Or even a bit more elegantly by defining a new geom_link_line function based on geom_segment.

geom_link_line <- function(mapping = NULL, data = links(), stat = "identity",
          position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE,
          ...){
  default_aes <- aes(y=y, yend=yend, x=(x+xend)/2, xend=(xmin+xmax)/2)
  mapping <- gggenomes:::aes_intersect(mapping, default_aes)

  layer(geom = GeomSegment, mapping = mapping, data = data, stat = stat, 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
        params = list(na.rm = na.rm, ...))
}

p1 + geom_link_line(aes(color=cluster_id))
TC-Hewitt commented 2 years ago

Thanks for this great answer. Does exactly what I was looking for and makes the plots much easier to interpret. The geom_link_line function is very convenient. Many thanks!