definir nível de fator em ggplot2

Sep 04 2020

Olá, eu precisaria de ajuda para classificar geom_segment no meu gráfico pela coluna end_scaffold.

Aqui está o código que usei para produzir o seguinte gráfico:

library(ggplot2)
#Here I try to sort the data in order to get geom_segment sorted in the plot but it does not work 
tab<-tab[with(tab, order(-end_scaff,-end_gene)), ] 


ggplot(tab, aes(x = start_scaff, xend = end_scaff, 
                y = molecule, yend = molecule)) +
  geom_segment(size = 3, col = "grey80") +
  geom_segment(aes(x = ifelse(direction == 1, start_gene, end_gene),
                   xend = ifelse(direction == 1, end_gene, start_gene)),
               data = tab, 
               arrow = arrow(length = unit(0.1, "inches")), size = 2) +
  geom_text(aes(x = start_gene, y = molecule, label = gene),
            data = tab, nudge_y = 0.2) + 
  scale_y_discrete(limits = rev(levels(tab$molecule))) +
  theme_minimal()

alguém tem uma ideia para ordenar geom_segmentpela coluna end_scaffold(decrescente) (onde scaffold_1254deveria estar no topo da trama e scaffold_74038deveria estar embaixo).

aqui estão os dados

> dput(tab)
structure(list(molecule = structure(c(2L, 6L, 6L, 3L, 7L, 4L, 
5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("", "scaffold_1254", "scaffold_15158", "scaffold_7180", 
"scaffold_74038", "scaffold_7638", "scaffold_8315"), class = "factor"), 
    gene = structure(c(8L, 6L, 5L, 3L, 7L, 4L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
    "G1", "G2", "G3", "G4", "G5", "G6", "G7"), class = "factor"), 
    start_gene = c(6708L, 9567L, 3456L, 10105L, 2760L, 9814L, 
    1476L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA), end_gene = c(11967L, 10665L, 4479L, 10609L, 
    3849L, 10132L, 2010L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA), start_scaff = c(1L, 1L, 
    1L, 1L, 1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA), end_scaff = c(20072, 15336, 
    15336, 13487, 10827, 10155, 2010, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), strand = structure(c(2L, 
    2L, 3L, 2L, 3L, 2L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "backward", 
    "forward"), class = "factor"), direction = c(-1L, -1L, 1L, 
    -1L, 1L, -1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA)), row.names = c(7L, 5L, 4L, 2L, 
6L, 3L, 1L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L), class = "data.frame")

Respostas

1 GGamba Sep 04 2020 at 15:30

Para uma solução dentro do ggplot, você pode remover o limitsdo scale_y_discrete(seria reordenado com base nos níveis dos fatores) e usar y = reorder(molecule, end_scaff)dentro do aes:

library(dplyr)
library(ggplot2)

tab <- tab %>% filter(!is.na(start_gene))

ggplot(tab, aes(x = start_scaff, xend = end_scaff, 
               y = reorder(molecule, end_scaff), yend = molecule)) +
  geom_segment(size = 3, col = "grey80") +
  geom_segment(aes(x = ifelse(direction == 1, start_gene, end_gene),xend = ifelse(direction == 1, end_gene, start_gene)),
               arrow = arrow(length = unit(0.1, "inches")), size = 2) +
  geom_text(aes(x = start_gene, y = molecule, label = gene), nudge_y = 0.2) + 
  scale_y_discrete() +
  theme_minimal()

Criado em 04-09-2020 pelo pacote reprex (v0.3.0)

1 RuiBarradas Sep 04 2020 at 15:21

O truque é para reordenar os níveis de molecule, não o todo data.frame. Em vez de

tab <- tab[with(tab, order(-end_scaff,-end_gene)), ]

corre

i <- with(tab, order(-end_scaff,-end_gene))
mol <- unique(tab$molecule[i]) tab$molecule <- factor(tab$molecule, levels = mol)

O mesmo código de plotagem agora produz o gráfico a seguir.