0

have data with 21 samples (in molecule column) and 157 unique elements in gene column (80% in common), so I create a data.frame with the detected elements (gene column) 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 in the rows (that contains all the detected elements) and create a extra column (labX, just a copy of the gene column) with NA in all samples except in genes rows (see in tail).

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 rows (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'))

enter image description here

with the previous code, the label are over the arrows, but I want the labels in X axis, something like:

enter image description here

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:

I will use gene column that is the same than labX but with characters for each row !!!

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") + 
scale_x_discrete(breaks = formatC(1:length(unique(gdf$gene)) , 
                 width = 2, flag = "0"), 
                 labels = unique(gdf$gene) )

and it didn't show the labels !!!

enter image description here

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

Any advise ??!!

thanks

abraham
  • 661
  • 8
  • 14
  • 1
    take a look at nudge_x and nudge_y. https://ggplot2.tidyverse.org/reference/geom_text.html - same duplicate for removing x axis question may be here: https://stackoverflow.com/questions/35090883/remove-all-of-x-axis-labels-in-ggplot – Eirik Fesker Mar 27 '23 at 22:10
  • 1
    Have you tried adding your labels as axis text via `scale_x_continuous`, i.e. you could set the positions for the labels via the `breaks` argument and the labels via the `labels` argument. – stefan Mar 27 '23 at 22:23
  • Thanks, I found the problem, and I did with scale_x_continuous, I just made a data.frame with the labels and the position in X for each label, Thanks so much !!!! – abraham Mar 30 '23 at 21:19

0 Answers0