0

I created this plot from the following data

> head(data.frame(ali))
  qname    qlen  qstart    qend strand         tname    tlen  tstart    tend  nmatch    alen mapq
1     1 2535400   16999 2505278      +  chromosome_1 2608419    3145 2582047 2228136 2615956   60
2     1 2535400 2514043 2521414      + chromosome_23  557589  348928  356301    6955    7373   23
3     1 2535400 2514040 2521414      + chromosome_13  959323  306762  314131    6489    7374    0
4     1 2535400     350    6278      -  chromosome_4 1629129 1048335 1054264    5373    5929    4
5     1 2535400     350    6278      + chromosome_11 1087446 1070616 1076544    5306    5928    0
6     1 2535400     350    6278      +  chromosome_4 1629129  886366  892296    5150    5930    0
> str(data.frame(ali))
'data.frame':   1156 obs. of  12 variables:
 $ qname : chr  "1" "1" "1" "1" ...
 $ qlen  : num  2535400 2535400 2535400 2535400 2535400 ...
 $ qstart: num  16999 2514043 2514040 350 350 ...
 $ qend  : num  2505278 2521414 2521414 6278 6278 ...
 $ strand: chr  "+" "+" "+" "-" ...
 $ tname : chr  "chromosome_1" "chromosome_23" "chromosome_13" "chromosome_4" ...
 $ tlen  : num  2608419 557589 959323 1629129 1087446 ...
 $ tstart: num  3145 348928 306762 1048335 1070616 ...
 $ tend  : num  2582047 356301 314131 1054264 1076544 ...
 $ nmatch: num  2228136 6955 6489 5373 5306 ...
 $ alen  : num  2615956 7373 7374 5929 5928 ...
 $ mapq  : num  60 23 0 4 0 0 22 0 15 0 ...

Coverage plot

What I would like is to order the 25 chromosomes on the left numerically, starting with chromosome 1 on the top and chromosome 25 on the bottom

For the legend I wanted to order the first 33 numbers numerically, once again starting with 1 and descending to 33, and then all the stuff afterwards (ie. bd_##x## and chloroplast) doesn't matter how it is ordered. It can stay the same or get switched around as long as it starts after 33.

Does anyone have any idea how to do this in ggplot?

This is all the code I used to make the plot

# import libraries
library(pafr, quietly = TRUE)
library(RColorBrewer)

# import alignment
alignment = system.file("extdata", "oldToNewMap.paf", package="pafr")
ali <- read_paf(alignment)

# create color palette
nb.cols <- 89
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)

# plot 
plot_coverage(ali, fill='qname') +
  scale_fill_manual(values = mycolors) 
  • 1
    They're being sorted alphanumerically. Add a leading 0 to the single digit values e.g. "chromosome 1" becomes "chromosome 01" and so on. Then `scale_y_reverse()` if they're not in the order you want. You can also change your "tname" column to `as.factor()` and assign levels based on the order you want them in if the labels have to remain the same. – L Tyrone Apr 01 '23 at 04:54

1 Answers1

0

The most immediately usable and flexible, but not necessarily most reproducible, way is to manually define the axis (see How do you specifically order ggplot2 x axis instead of alphabetical order?):

dat<-data.frame(x=c("chromosome 13","chromosome 1","chromosome 4","whocares"),y=c(3,4,5,10))

ggplot(dat,aes(x=x,y=y))+
  geom_col()

enter image description here

ggplot(dat,aes(x=x,y=y))+
  geom_col()+
  scale_x_discrete(limits=c("chromosome 1","chromosome 4","chromosome 13","whocares"))

enter image description here

You can also define the order of factor levels (from: https://stackoverflow.com/a/48998414/9096420):

level_order=c("chromosome 1","chromosome 4","chromosome 13","whocares")

ggplot(dat,aes(x=factor(x,level=level_order),y=y))+
  geom_col()

enter image description here

You could do something with regular expressions, if you wanted to make this more reproducible to other projects, but that might be more complicated than you want for this one.

For example, @Leroy Tyrone suggested adding leading 0s:

dat<-data.frame(x=c("chromosome_13","chromosome_1","chromosome_4","whocares"),y=c(3,4,5,10))
dat$x<-gsub("chromosome_([0-9]{1})$","chromosome_0\\1",dat$x)

> dat$x
[1] "chromosome_13" "chromosome_01" "chromosome_04" "whocares" 

ggplot(dat,aes(x=x,y=y))+
  geom_col()

enter image description here

Dylan_Gomes
  • 2,066
  • 14
  • 29