Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw links as single lines? #129

Closed
TC-Hewitt opened this issue Oct 7, 2022 · 4 comments
Closed

Draw links as single lines? #129

TC-Hewitt opened this issue Oct 7, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@TC-Hewitt
Copy link

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
Copy link
Author

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
Copy link
Owner

thackl commented Oct 15, 2022

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
Copy link
Owner

thackl commented Oct 15, 2022

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))

@thackl thackl added the enhancement New feature or request label Oct 15, 2022
@TC-Hewitt
Copy link
Author

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!

@thackl thackl closed this as completed in 7a0b738 Oct 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants