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

[feature request] arrow shape - aesthetics strand or direction #28

Open
acpguedes opened this issue Aug 4, 2020 · 7 comments
Open

[feature request] arrow shape - aesthetics strand or direction #28

acpguedes opened this issue Aug 4, 2020 · 7 comments

Comments

@acpguedes
Copy link

Hi,

I don't know if I missed an option, but I suggest an aesthetics to create an arrow according to the strand feature.
geom_gene_arrow(aes(direction = strand))

The effect of this aesthetics must be the direction of the arrow, this way it may support non-overlapping genes in the same y axis.

I don't know how to represent when it has overlap.

Another suggestion, to better fit the name of the molecule, is an option where the text might be placed below to arrow representation.

'>>gen1>>##>>>>>>gen2>>>>>>###<<<<<<<gen3<<<<<<<##>>>>>gen4>>>>>>'
'Melecule name or text'

@wilkox
Copy link
Owner

wilkox commented Aug 9, 2020

Hi Aureliano,

Re. your first suggestion, if I understand correctly this functionality is already provided by the forward aesthetic:

library(ggplot2)
library(gggenes)

example_genes$direction <- ifelse(example_genes$strand == "forward", 1, -1)
ggplot(subset(example_genes, molecule == "Genome1"),
                aes(xmin = start, xmax = end, y = strand, fill = gene,
                    forward = direction)) +
  geom_gene_arrow() +
  theme_genes()

Created on 2020-08-09 by the reprex package (v0.3.0)

Re. your second suggestion, this is a good idea although might be a little tricky to implement as the area available below the gene arrows is not as well defined as the area inside them. I'll have a look at whether there is a practical way to do this.

@rickbeeloo
Copy link

+1 it would be really nice to plot both forward and reverse genes on the same y-axis, rather than on separate strands in the provided example.

@acpguedes for the labels you can try different things, for example using ggrepel (note I just quickly wrote this and can be plotted way nicer with some adjustments):

ggplot(example_genes, aes(xmin = start, xmax = end, y = molecule, fill = gene)) +
  geom_gene_arrow() +
  facet_wrap(~ molecule, scales = "free", ncol = 1) +
  scale_fill_brewer(palette = "Set3") +
  theme_genes() + 
  ggrepel::geom_text_repel(data = example_genes, aes(x = start, y = molecule, label = gene), direction = 'y', inherit.aes = F, nudge_y = -1, show.legend = F) + guides(fill = F)

image

@acpguedes
Copy link
Author

@rickbeeloo nice, I like this alternative. Thanks for the suggestion

@wilkox
Copy link
Owner

wilkox commented Oct 11, 2020

@rickbeeloo I might be misunderstanding what you're trying to do, but the forward aesthetic supports drawing a mix of gene directions on the same molecule:

library(tidyverse)
#> Warning: replacing previous import 'vctrs::data_frame' by 'tibble::data_frame'
#> when loading 'dplyr'
library(gggenes)

example_genes %>%
  mutate(direction = sample(c(-1, 1), 72, T)) %>%
  ggplot(aes(xmin = start, xmax = end, y = molecule, fill = gene, forward = direction)) +
  geom_gene_arrow() +
  facet_wrap(~ molecule, scales = "free", ncol = 1) +
  scale_fill_brewer(palette = "Set3") +
  theme_genes()

Created on 2020-10-11 by the reprex package (v0.3.0)

@rickbeeloo
Copy link

Aaah this is exactly what I meant! Awesome. Perhaps useful to add this example to the documentation at it will be more common than plotting in the same direction or on separate "molecules"

@rickbeeloo
Copy link

rickbeeloo commented Oct 14, 2020

Update
@wilkox I saw you referenced this in another question so I made some adjustments that may suit other cases.

Plot labels above genes using geom_text

  • We move the labels above the gene using nudge_y
  • Move them to the middle of the arrow (start + end) /2
ggplot(example_genes, aes(xmin = start, xmax = end, y = molecule, fill = gene)) +
  geom_gene_arrow() +
  facet_wrap(~ molecule, scales = "free", ncol = 1) +
  scale_fill_brewer(palette = "Set3") +
  theme_genes() + 
  geom_text(data=example_genes %>% mutate(start = (start + end)/2), aes(x=start, label = gene), nudge_y = 0.2 )

image

Plot labels above genes using ggrepel::geom_text (small genes)

ggplot(example_genes, aes(xmin = start, xmax = end, y = molecule, fill = gene)) +
  geom_gene_arrow() +
  facet_wrap(~ molecule, scales = "free", ncol = 1) +
  scale_fill_brewer(palette = "Set3") +
  theme_genes() + 
  ggrepel::geom_text_repel(data = example_genes %>% mutate(start = (start + end)/2), aes(x = start, y = molecule, label = gene), inherit.aes = F, nudge_y = 1)

image

Plot labels above genes using ggrepel:geom_text (large labels)

# Simulate long labels
example_genes$gene <- paste0('qqqqqqqqqqqqq', example_genes$gene)

In this case we add a newline (\n) after every n characters, in this case 10, using gsub:

# Add a newline after every 10 characters
example_genes$gene2 <- gsub('(?=(?:.{10})+$)', "\n", example_genes$gene, perl = TRUE)

ggplot(example_genes, aes(xmin = start, xmax = end, y = molecule, fill = gene)) +
  geom_gene_arrow() +
  facet_wrap(~ molecule, scales = "free", ncol = 1) +
  scale_fill_brewer(palette = "Set3") +
  theme_genes() + 
  ggrepel::geom_text_repel(data = example_genes %>% mutate(start = (start + end)/2), 
                           aes(x = start, y = molecule, label = gene2), inherit.aes = F, nudge_y = 0.5, lineheight = 0.6)

image

@wilkox
Copy link
Owner

wilkox commented Oct 17, 2020

Perhaps useful to add this example to the documentation at it will be more common than plotting in the same direction or on separate "molecules"

@rickbeeloo Good idea, I've updated the example data and the README with a more useful example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants