abraham
abraham

Reputation: 743

How to plot the x labels and scale bar using ggplot2 and gggenes?

I have 3 data.frames with the data to plot genes. The problem that I want to solve, is to keep the genes legends in x (geneA to geneF), as well as the scale bar with the numbers in x, I have tried the next code; maybe the problem is scale_x_continuous, when I don’t use it the scale bar is ok, but I want the genes labels also in x (or could be in top!!!)

df1

     sample  gene width start   end orientation
1  GENOME326 geneA 15630     1 15630       FALSE
2  GENOME326 geneC   396 15631 16026       FALSE
3  GENOME326 geneF   360 16027 16386       FALSE
4  GENOME300 geneA 15630     1 15630       FALSE
5  GENOME300 geneC   396 15631 16026       FALSE
6  GENOME300 geneF   360 16027 16386       FALSE
7  GENOME594 geneA 15630     1 15630       FALSE
8  GENOME594 geneC   396 15631 16026       FALSE
9  GENOME594 geneF   360 16027 16386       FALSE
10 GENOME326 geneB  2106 16826 18931        TRUE
11 GENOME326 geneD  1362 18932 20293        TRUE
12 GENOME326 geneE  2169 20294 22462        TRUE
13 GENOME300 geneB  2106 16826 18931        TRUE
14 GENOME300 geneD  1362 18932 20293        TRUE
15 GENOME300 geneE  2169 20294 22462        TRUE
16 GENOME594 geneB  2106 16826 18931        TRUE
17 GENOME594 geneD  1362 18932 20293        TRUE
18 GENOME594 geneE  2169 20294 22462        TRUE

df2

  gene width start   end position  n
1 geneA 15630     1 15630   7815.5 21
2 geneC   396 15631 16026  15828.5 21
3 geneF   360 16027 16386  16206.5 21
4 geneB  2106 16387 18492  17439.5 21
5 geneD  1362 18493 19854  19612.5 21
6 geneE  2169 19855 22023  20939.0 21

df3

      sample  gene start   end domains from   to orientation       gap
1  GENOME326 geneA     1 15627 DomainA 8587 9751        TRUE      <NA>
2  GENOME326 geneA     1 15627 DomainB 6903 8541        TRUE      <NA>
3  GENOME326 geneA     1 15627 DomainC 6013 6913        TRUE      <NA>
4  GENOME326 geneA     1 15627 DomainD 5053 5778        TRUE      <NA>
5  GENOME326 geneA     1 15627 DomainE 3421 4833        TRUE      <NA>
6  GENOME326 geneA     1 15627 DomainF 3097 3420        TRUE      <NA>
7  GENOME300 geneA     1 15627 DomainA 8587 9751        TRUE      <NA>
8  GENOME300 geneA     1 15627 DomainB 6903 8541        TRUE      <NA>
9  GENOME300 geneA     1 15627 DomainC 6013 6913        TRUE      <NA>
10 GENOME300 geneA     1 15627 DomainD 5053 5778        TRUE      <NA>
11 GENOME300 geneA     1 15627 DomainE 3421 4833        TRUE      <NA>
12 GENOME300 geneA     1 15627 DomainF 3097 3420        TRUE      <NA>
13 GENOME594 geneA     1 15627 DomainA 8587 9751        TRUE      <NA>
14 GENOME594 geneA     1 15627 DomainB 6903 8541        TRUE      <NA>
15 GENOME594 geneA     1 15627 DomainC 6013 6913        TRUE      <NA>
16 GENOME594 geneA     1 15627 DomainD 5053 5778        TRUE      <NA>
17 GENOME594 geneA     1 15627     GAP 3421 4921        TRUE ---GAP---
18 GENOME594 geneA     1 15627 DomainF 3097 3420        TRUE      <NA>

code:

ggplot() +
    # rtx gene  
    geom_gene_arrow(data =df1, 
                    aes(xmin=start, xmax=end, y= sample, forward=orientation), 
                    fill="white", size=0.5, color="black",
                    arrowhead_width = grid::unit(11, "mm"),
                    arrowhead_height = grid::unit(11, "mm"),
                    arrow_body_height = grid::unit(9, "mm")) +
    scale_x_continuous(expand = expansion(mult = c(0.002, 0.001)), # 
                       position = "bottom",
                       breaks = df2$position,                      #  
                       labels = df2$gene) + 
    scale_shape_manual(values = c(15, 16)) +
    # Domains
    geom_subgene_arrow(data = df3, 
                       aes(xsubmin = from, xsubmax = to, fill = domains, xmin=start, xmax=end, y=sample), alpha=0.5,
                       arrowhead_width = unit(15, "mm"),
                       arrowhead_height = unit(7, "mm"),
                       arrow_body_height = unit(7.4, "mm")) + 
    geom_text(data = subset(df3, gap != "NA") %>% mutate(posgap = (from + 250) ), #  
              aes(y=sample, x=posgap, label=gap), angle = 0, hjust = 0, size = 5, color="black") +
    # agregar color a cada dominio de manera individual 
    scale_fill_manual(labels = list(expression(DomainA),
                                    expression(DomainB), 
                                    expression(DomainC),  #  
                                    expression(DomainD), 
                                    expression(DomainE), 
                                    expression(DomainF), 
                                    expression(GAP) ),
                      values = c( "dodgerblue3", "#FB8072", "#FDB462", "#BC80BD", "#FFED6F", "#9997FC", "grey50") ) + 
    xlab("base pair (bp)") + 
    scale_y_discrete(position="right") + theme_classic() +
    theme(panel.background = element_rect(fill = 'white', color = 'white'), # 
          axis.text.y = element_text(size = 14, family="Times New Roman", face="bold"), 
          panel.border = element_blank(),
          panel.grid = element_blank(),
          panel.spacing = element_blank(),
          axis.title.y= element_blank(), 
          # axis.ticks.x = element_blank(), #  
          axis.ticks.y = element_blank(), #   
          axis.title.x = element_text(size = 10, face = "bold"),
          axis.text.x = element_text(angle = 90, 
                                     vjust = 0.5, 
                                     hjust=1, 
                                     size = 15, # 
                                     face="bold.italic", # 
                                     family="Times New Roman"), 
          legend.text=element_text(size=14),
          legend.position="bottom",         #  
          legend.title=element_blank(),     #  
    ) +  
    guides(fill=guide_legend(nrow=1))       #  

Without scale_x_continuous, but the genes in x do not appear !!!

ggplot() +
    # rtx gene  
    geom_gene_arrow(data =df1, 
                    aes(xmin=start, xmax=end, y= sample, forward=orientation), 
                    fill="white", size=0.5, color="black",
                    arrowhead_width = grid::unit(11, "mm"),
                    arrowhead_height = grid::unit(11, "mm"),
                    arrow_body_height = grid::unit(9, "mm")) +
    scale_shape_manual(values = c(15, 16)) +
    # Domains 
    geom_subgene_arrow(data = df3, 
                       aes(xsubmin = from, xsubmax = to, fill = domains, xmin=start, xmax=end, y=sample), alpha=0.5,
                       arrowhead_width = unit(15, "mm"),
                       arrowhead_height = unit(7, "mm"),
                       arrow_body_height = unit(7.4, "mm")) + 
    geom_text(data = subset(df3, gap != "NA") %>% mutate(posgap = (from + 250) ), #  
              aes(y=sample, x=posgap, label=gap), angle = 0, hjust = 0, size = 5, color="black") +
    # agregar color a cada dominio de manera individual 
    scale_fill_manual(labels = list(expression(DomainA),
                                    expression(DomainB), 
                                    expression(DomainC),  #  
                                    expression(DomainD), 
                                    expression(DomainE), 
                                    expression(DomainF), 
                                    expression(GAP) ),
                      values = c( "dodgerblue3", "#FB8072", "#FDB462", "#BC80BD", "#FFED6F", "#9997FC", "grey50") ) + 
    xlab("base pair (bp)") + 
    scale_y_discrete(position="right") + theme_classic() +
    theme(panel.background = element_rect(fill = 'white', color = 'white'), # 
          axis.text.y = element_text(size = 14, family="Times New Roman", face="bold"), 
          panel.border = element_blank(),
          panel.grid = element_blank(),
          panel.spacing = element_blank(),
          axis.title.y= element_blank(), 
          # axis.ticks.x = element_blank(), #  
          axis.ticks.y = element_blank(), #   
          axis.title.x = element_text(size = 10, face = "bold"),
          axis.text.x = element_text(angle = 90, 
                                     vjust = 0.5, 
                                     hjust=1, 
                                     size = 15, # 
                                     face="bold.italic", # 
                                     family="Times New Roman"), 
          legend.text=element_text(size=14),
          legend.position="bottom",         #  
          legend.title=element_blank(),     #  
    ) +  
    guides(fill=guide_legend(nrow=1))       #  

plots: enter image description here enter image description here

Any solution ?

Thanks

Upvotes: 0

Views: 63

Answers (0)

Related Questions