# How to order grouped elements in Gviz

Gviz is useful for visualization of gene transcripts by genomic coordinates. I'm using it to show the difference between splice variant transcripts. However, it seems that the ordering of transcript elements cannot be controlled by the parameters given in Gviz documentation - and yet I would like to order them by name.

I have tried to set the grouping column as factors, but this does not change the ordering, as illustrated below:

library(GenomicRanges)
library(Gviz)

example_gr <- GRanges(seqnames = Rle(rep("chr1", 6)),
ranges = IRanges(start = c(1, 12, 1, 10, 4, 12),
end = c(9, 19, 5, 19, 8, 19),
names = head(letters, 6)),
strand = Rle(rep("+", 6)),
group = factor(x = c("g1", "g1", "g2", "g2", "g3", "g3"),
levels = c("g2", "g1", "g3")), #Order
id = head(letters, 6))

example_artrack <- AnnotationTrack(example_gr,
shape="box",
groupAnnotation = "group",
collapse = FALSE,
showFeatureId = TRUE)
example_gat <- GenomeAxisTrack(name = "chr1")
plotTracks(list(example_gat, example_artrack))


Notice that the order does not follow the levels of the group factor. It is also not inherent to the order given in the GRange object, because I've tried reversing the object with rev() and the plot was identical. I can get around this problem by specifying separate tracks for each group. However, this feels wrong to me - it ought to be possible using the grouping, I think, and it's not as clean when I have to specify a separate track for 15 different transcripts.

example_artrack1 <- AnnotationTrack(example_gr[example_gr$$group == "g1"], shape="box", groupAnnotation = "group", collapse = FALSE, showFeatureId = TRUE) example_artrack2 <- AnnotationTrack(example_gr[example_gr$$group == "g2"],
shape="box",
groupAnnotation = "group",
collapse = FALSE,
showFeatureId = TRUE)
example_artrack3 <- AnnotationTrack(example_gr[example_gr\$group == "g3"],
shape="box",
groupAnnotation = "group",
collapse = FALSE,
showFeatureId = TRUE)
plotTracks(list(example_gat, example_artrack2, example_artrack1, example_artrack3))


(As an aside, I am wondering why the line that connects the groups has a gap - I have never seen this before, but perhaps it's due to zooming in so far?)

Can the order can be manipulated outside of specifying separate tracks?