在ggplot2中更改条形的顺序

时间:2014-01-07 15:35:19

标签: r

我有一个关于在ggplot2中订购条形的问题。 这是我的原始表,名为data:

Enrichment  Term
7.078947368 Chaperonin Cpn60/TCP-1
5.309210526 ATPase, AAA-type
5.22358156  Proteasome
4.822134387 Val, Ile, Leu degradation
4.555128205 Cellular respiration
4.129591837 Carboxylic acid binding
3.814380044 NAD
3.649206349 tRNA metabolism
3.632200699 Mitochondria
3.483333333 Anion transport
3.15454932  Electron carrier activity
3.047916667 Generation of precursor metabolites/energy
-2.045706795    Chromatin modification
-2.611885283    Zinc finger, C2H2-type
-2.844052863    Zinc finger, PHD-type
-3.022150624    Chromatin remodeling complex
-3.231878254    SANT (histone-tail-binding) domain
-3.984612664    Bromodomain
-5.251256281    Development of respiratory system

我使用了这段代码:

    ggplot(data, aes(x=Term, y=Enrichment)) +  geom_bar(stat="identity", width=.5) +  coord_flip()

我希望像表格中那样排序吧(对于浓缩列),但它会自动按字母顺序排序。我已经尝试了不同的东西,但由于我是R的初学者,我无法理解。

非常感谢!

1 个答案:

答案 0 :(得分:1)

尝试在reorder(Term, -Enrichment)中使用Term代替ggplot


完整的代码:

data <- read.table(text = "Enrichment  Term
7.078947368 'Chaperonin Cpn60/TCP-1'
5.309210526 'ATPase, AAA-type'
5.22358156  Proteasome
4.822134387 'Val, Ile, Leu degradation'
4.555128205 'Cellular respiration'
4.129591837 'Carboxylic acid binding'
3.814380044 NAD
3.649206349 'tRNA metabolism'
3.632200699 Mitochondria
3.483333333 'Anion transport'
3.15454932  'Electron carrier activity'
3.047916667 'Generation of precursor metabolites/energy'
-2.045706795    'Chromatin modification'
-2.611885283    'Zinc finger, C2H2-type'
-2.844052863    'Zinc finger, PHD-type'
-3.022150624    'Chromatin remodeling complex'
-3.231878254    'SANT (histone-tail-binding) domain'
-3.984612664    Bromodomain
-5.251256281    'Development of respiratory system",header = TRUE)


library(ggplot2)
ggplot(data, aes(x=reorder(Term, -Enrichment), y=Enrichment)) +  
  geom_bar(stat="identity", width=.5) +  
  coord_flip()

enter image description here