2017-10-11 26 views
-4

私はデータフレームを準備し、彼にggplotを使用しています。しかし、最初の注文は尊重されません。どうやってこの注文を尊重することができますかgeom_bar(stat = "identity")を使ってggplotを並べ替え

Patient Nb_peptides Type_affinite 
1  22   563    a 
2  22  1040    b 
3  22  11139    c 
4  24   489    a 
5  24  1120    b 
6  24  11779    c 
7  13   467    a 
8  13  1239    b 
9  13  14600    c 


g_plot <- ggplot(data = nb_peptides_type, 
       aes(x = reorder(Patient, True_order), 
        y = Nb_peptides, 
        fill = Type_affinite)) + 
    geom_bar(stat = "identity") 

print(g_plot) 

enter image description here

+0

「True_order」は何ですか?私はあなたのコードでそれを参照してください... –

答えて

0

やすいように、スタンドアローンのコードを入力してください。

私は、因子レベルを並べ替えるためにプロットの外にlevelsを使用します:それはあなたが探しているものですか?

## fake dataframe 
df <- data.frame(patient = as.factor(rep((21:30), each=3)), 
       nb = rpois(30, 1000), 
       type=sample(letters[1:3], replace =T, size =30)) 

## initial plot 
ggplot(data = df, 
     aes(x = patient, 
      y = nb, 
      fill = type)) + 
    geom_bar(stat = "identity") 

## adjust factors levels 
True_order <- sample((21:30), 10) 
levels(df$patient) <- True_order 


## re-plot 
ggplot(data = df, 
     aes(x = patient, 
      y = nb, 
      fill = type)) + 
    geom_bar(stat = "identity") 
関連する問題