Giter Site home page Giter Site logo

order of the genome names about gggenes HOT 5 CLOSED

wilkox avatar wilkox commented on August 10, 2024
order of the genome names

from gggenes.

Comments (5)

ramadatta avatar ramadatta commented on August 10, 2024 1

Thank you so much for this @wilkox. This is what I needed.

from gggenes.

wilkox avatar wilkox commented on August 10, 2024

Hi ramadatta, you can set the order of the genomes (or indeed any categorical variable mapped to an axis) by making that variable a factor.

If this doesn't work or you need some more help, could you post the code you already have as a reprex?

from gggenes.

ramadatta avatar ramadatta commented on August 10, 2024

Thanks much @wilkox . Will come back to you soon on this.

from gggenes.

ramadatta avatar ramadatta commented on August 10, 2024
library(ggplot2)
library(gggenes)

dummies <- make_alignment_dummies(
  example_genes,
  aes(xmin = start, xmax = end, y = molecule, id = gene),
  on = "genE"
)

ggplot(example_genes, aes(xmin = start, xmax = end, y = factor(molecule), fill = gene)) +
  geom_gene_arrow() +
  geom_blank(data = dummies) +
  facet_wrap(~ molecule, scales = "free", ncol = 1) +
  scale_fill_brewer(palette = "Set3") +
  theme_genes()

# Creating a presence/absence matrix for example genes

PA_matrix <- as.data.frame(with(example_genes, table(molecule, gene)) > 0L) +0L
PA_matrix
#>         genA genB genC genD genE genF protA protB protC protD protE protF
#> Genome1    1    1    1    1    1    1     0     0     1     1     1     1
#> Genome2    1    1    1    1    1    1     1     1     0     0     0     0
#> Genome3    1    1    1    1    1    1     1     1     0     0     0     0
#> Genome4    1    1    1    1    1    1     0     0     0     0     0     0
#> Genome5    1    1    1    1    1    1     0     0     1     1     1     1
#> Genome6    1    1    1    1    1    1     1     1     0     0     0     0
#> Genome7    0    1    1    1    1    1     1     1     1     1     1     1
#> Genome8    0    1    1    1    1    1     1     1     1     1     1     1

# Sorting the presence/absence matrix for example genes

sorted_PA_matrix <- PA_matrix[do.call(order,as.data.frame(PA_matrix)),]
sorted_PA_matrix
#>         genA genB genC genD genE genF protA protB protC protD protE protF
#> Genome7    0    1    1    1    1    1     1     1     1     1     1     1
#> Genome8    0    1    1    1    1    1     1     1     1     1     1     1
#> Genome4    1    1    1    1    1    1     0     0     0     0     0     0
#> Genome1    1    1    1    1    1    1     0     0     1     1     1     1
#> Genome5    1    1    1    1    1    1     0     0     1     1     1     1
#> Genome2    1    1    1    1    1    1     1     1     0     0     0     0
#> Genome3    1    1    1    1    1    1     1     1     0     0     0     0
#> Genome6    1    1    1    1    1    1     1     1     0     0     0     0

sorted_genomes <- row.names(sorted_PA_matrix)
sorted_genomes
#> [1] "Genome7" "Genome8" "Genome4" "Genome1" "Genome5" "Genome2" "Genome3"
#> [8] "Genome6"

# Creating sorted_dummies and sorted_example_genes which the final output figure should reflect
sorted_dummies <- dummies[order(unlist(sapply(dummies$molecule, function(x) which(sorted_genomes == x)))),]
sorted_example_genes <- example_genes[order(unlist(sapply(example_genes$molecule, function(x) which(sorted_genomes == x)))),]

#head(example_genes)
#head(sorted_example_genes)

ggplot(sorted_example_genes, aes(xmin = start, xmax = end, y = factor(molecule), fill = gene)) +
  geom_gene_arrow() +
  geom_blank(data = sorted_dummies) +
  facet_wrap(~ molecule, scales = "free", ncol = 1) +
  scale_fill_brewer(palette = "Set3") +
  theme_genes()

Hi @wilkox ,

Thank you very much for passing the reprex link. It was useful.

Accordingly, I need the final output figure above in the order of the genomes found in the "sorted_PA_matrix". Passing sorted_examples_genes and sorted_dummies and using factor(molecule), could not help me generate the correct order of genomes intended. Can request to know if I am missing something here? Many thanks in advance.

from gggenes.

wilkox avatar wilkox commented on August 10, 2024
library(ggplot2)
library(gggenes)

dummies <- make_alignment_dummies(
  example_genes,
  aes(xmin = start, xmax = end, y = molecule, id = gene),
  on = "genE"
)

# Creating a presence/absence matrix for example genes
PA_matrix <- as.data.frame(with(example_genes, table(molecule, gene)) > 0L) +0L

# Sorting the presence/absence matrix for example genes
sorted_PA_matrix <- PA_matrix[do.call(order,as.data.frame(PA_matrix)),]

sorted_genomes <- row.names(sorted_PA_matrix)

# Creating sorted_dummies and sorted_example_genes which the final output figure should reflect
sorted_dummies <- dummies[order(unlist(sapply(dummies$molecule, function(x) which(sorted_genomes == x)))),]
sorted_example_genes <- example_genes[order(unlist(sapply(example_genes$molecule, function(x) which(sorted_genomes == x)))),]

# Convert molecule variable to a factor
sorted_example_genes$molecule <- factor(sorted_example_genes$molecule, levels = unique(sorted_example_genes$molecule))
sorted_dummies$molecule <- factor(sorted_dummies$molecule, levels = unique(sorted_dummies$molecule))

ggplot(sorted_example_genes, aes(xmin = start, xmax = end, y = factor(molecule), fill = gene)) +
  geom_gene_arrow() +
  geom_blank(data = sorted_dummies) +
  facet_wrap(~ molecule, scales = "free", ncol = 1) +
  scale_fill_brewer(palette = "Set3") +
  theme_genes()

Created on 2020-07-08 by the reprex package (v0.3.0.9001)

from gggenes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.