gggenes
gggenes copied to clipboard
[feature request] arrow shape - aesthetics strand or direction
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'
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.
+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)
@rickbeeloo nice, I like this alternative. Thanks for the suggestion
@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)
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"
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 )
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)
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)
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