how to plot a bar chart with non-sorted x-axis (lattice)

前端 未结 1 1956
悲哀的现实
悲哀的现实 2021-01-29 12:33

My code are showed as follows:

read.table(\"GO.txt\",header=T,sep=\',\')->go
library(lattice)
barchart(go[,1]~go[,2],horiz=F,ylim=c(30,29666),
layout=c(1,1),s         


        
1条回答
  •  失恋的感觉
    2021-01-29 13:02

    "Lattice" took your factor variables and arranged them in default (alphabetical) order. You can change the order. Perhaps:

    go[,2] <- factor( go[,2] unique(as.character(go[,2])) )
    

    This should set the levels in the same order as they would first appear if you were paging through them. Another equivalent, although more Baroque, assignment would be:

    go[,2] <- factor( go[,2] as.character( go[,2][!duplicated(go[,2])]) )
    

    Then do your plot. (I did not attempt to input your data. They looked to be too messy. Well, actually I did give it a shot with tabs as the separator. If they once had tabs. then they lost them, and there are variable numbers of elements if white-space is the separator.)

    Using the data in the duplicate question the labels are not sorted:

    go[[2]] <- factor(go[[2]], levels=unique(as.character(go[[2]])))
    

    enter image description here

    0 讨论(0)
提交回复
热议问题