Hi @Limin_Chen,
Looks like your df1$chromosome column is stored as a factor with alphabetically sorted levels, which are then used in deciding the (incorrect) x-axis plotting order.
For illustration, contrast these definitions of a fictitious factor:
factor(c(1,2,3,10,11,12,21,22,23))
#> [1] 1 2 3 10 11 12 21 22 23
#> Levels: 1 2 3 10 11 12 21 22 23
factor(c("1","2","3","10","11","12","21","22","23"))
#> [1] 1 2 3 10 11 12 21 22 23
#> Levels: 1 10 11 12 2 21 22 23 3
factor(c("chr01","chr02","chr03","chr10","chr11","chr12","chr21","chr22","chr23"))
#> [1] chr01 chr02 chr03 chr10 chr11 chr12 chr21 chr22 chr23
#> Levels: chr01 chr02 chr03 chr10 chr11 chr12 chr21 chr22 chr23
Created on 2021-10-19 by the reprex package (v2.0.1)
You should be able to fix this by modifying your data frame as follows:
df1$chromosome <- factor(ifelse(chromosome %in% as.character(1:9),
paste0("chr0", chromosome),
paste0("chr", chromosome)))
Edit: Another option is to leave the factor levels containing digits only but produce a new ordered factor with the levels sorted correctly:
df1$chromosome <- factor(df1$chromosome,
ordered=TRUE,
levels=sort(as.numeric(levels(df1$chromosome))))