wilkox/gggenes

add sigle label in x axis !!!

cabraham03 opened this issue · 1 comments

I have data with 21 samples and 157 genes (80% in common), so I create a data.frame with the detected genes for each sample, but I just want to add a single label in X axis to avoid multiples and repetitive labels in all the plot, so I add a extra samples with the name of genes (that contains all the detected genes) and create a extra column (labX) with NA in all samples except in genes (last element).


head(gdf)
    molecule          gene width start  end labX
1 SAMPLE-300 BJE04_RS21740  3070     1 3070 <NA>
2 SAMPLE-300 BJE04_RS21750  1071  3071 4141 <NA>
3 SAMPLE-300 BJE04_RS21780   330  4142 4471 <NA>
4 SAMPLE-300 BJE04_RS21785  1629  4472 6100 <NA>
5 SAMPLE-300 BJE04_RS21790   243  6101 6343 <NA>
6 SAMPLE-300          cheA  2226  6344 8569 <NA>

tail(gdf)
     molecule        gene width  start    end        labX
3257    genes   wbjD/wecB  1080 175076 176155   wbjD/wecB
3258    genes        mshA   471 176156 176626        mshA
3259    genes VV1_RS01700   669 176627 177295 VV1_RS01700
3260    genes VV1_RS01705   513 177296 177808 VV1_RS01705
3261    genes VV1_RS15585   282 177809 178090 VV1_RS15585
3262    genes VV1_RS15620   879 178091 178969 VV1_RS15620

You can see that only genes (in molecule column) presented the labels in column labX, so to make the plot:

ggplot(gdf, aes(xmin = start, xmax = end, y = molecule) ) + 
    geom_gene_arrow(arrowhead_width = grid::unit(7, "mm"), 
                    arrowhead_height = grid::unit(7, "mm"), 
                    arrow_body_height = grid::unit(5.7, "mm"), size=0.4 ) + 
    scale_y_discrete(position="right") +
    geom_text(data=gdf %>% mutate(start = (start + end)/2),
              aes(x=start, label = labX), angle=45) + 
    theme(panel.background = element_rect(fill = 'white', color = 'white'))

Screen Shot 2023-03-27 at 12 21 51 PM

with the previous code the label are over the genes (last element), but I want the labels in X axis, something like:

Screen Shot 2023-03-27 at 12 42 37 PM

Move the label (in the green arrow) to x.axis (blue arrow) and eliminate the numbers (red arrow)

I tried to use scale_x_discrete

ggplot(gdf, aes(xmin = start, xmax = end, y = molecule) ) + 
    geom_gene_arrow(arrowhead_width = grid::unit(7, "mm"), 
                    arrowhead_height = grid::unit(7, "mm"), 
                    arrow_body_height = grid::unit(5.7, "mm"), size=0.4 ) + 
    scale_y_discrete(position="right") + 
    theme(panel.background = element_rect(fill = 'white', color = 'white')) + 
    scale_x_discrete(labels=gdf$labX)

but the labels did not appear in X, just disappear !!!

x

I think that maybe the problem is expand the limits !!

Any advise ??!!

thanks

Hi Abraham,

Here's an approach you could try: add an extra molecule where you don't draw the gene arrows, but only draw labels. You will have to tweak the plot theme and aesthetics to get it looking exactly as you want.

library(tidyverse)
library(gggenes)

# Plot with five identical genomes and a "dummy" genome
identical_genomes <- example_genes %>%
  filter(molecule == "Genome1") %>%
  nest(data = -molecule) %>%
  expand(molecule = c("Genome1", "Genome2", "Genome3", "Genome4", "Genome5"), data) %>%
  unnest(data)

ggplot(identical_genomes, aes(xmin = start, xmax = end, y = molecule, fill = gene)) +
  geom_gene_arrow() +
  scale_fill_brewer(palette = "Set3") +
  theme_genes()

# Add a 'labels' genome for the gene names
labels <- identical_genomes %>%
  filter(molecule == "Genome1") %>%
  mutate(molecule = "")

ggplot() +
  geom_gene_arrow(data = identical_genomes, 
                  aes(xmin = start, xmax = end, y = molecule, fill = gene)) +
  geom_gene_label(data = labels,
                  aes(xmin = start, xmax = end, y = molecule, label = gene)) +
  scale_fill_brewer(palette = "Set3") +
  theme_genes()

Created on 2023-03-28 with reprex v2.0.2