2017-06-05 80 views
-1

我正在嘗試使用ggplot2創建瀑布圖。我的數據:如何正確地對軸上的標籤進行排序?

> PctCtr 
    Index Volatility sign id  end  start 
1 WMT 0.04894867 pos 1 0.04894867 0.00000000 
2 MMM 0.09225684 pos 2 0.14120551 0.04894867 
3 AIG 0.18997607 pos 3 0.33118159 0.14120551 
4 AAPL 0.11067473 pos 4 0.44185632 0.33118159 
5  KO 0.05972203 pos 5 0.50157835 0.44185632 
6 COST 0.06088945 pos 6 0.56246780 0.50157835 
7  C 0.16428627 pos 7 0.72675407 0.56246780 
8 AMZN 0.12803944 pos 8 0.85479351 0.72675407 
9 ICE 0.09104894 pos 9 0.94584245 0.85479351 
10 VTR 0.05415755 pos 10 1.00000000 0.94584245 
11 Total 1.00000000 pos 11 0.00000000 1.00000000 

而且我的代碼:

GF = ggplot(PctCtr, aes(Volatility, fill = sign)) + 
    geom_rect(aes(Index, 
       xmin = id - 0.475, 
       xmax = id + 0.475, 
       ymin = end, 
       ymax = start)) + 
    scale_y_continuous(labels = percent)  
    scale_x_discrete("", breaks = levels(PctCtr$Index), labels = levels(PctCtr$Index)) 

GF 

生成的圖表: enter image description here

正如你所看到的,在x軸的名稱混亂。我認爲這是因爲levels()功能,當我看着它,它返回按字母順序排序值:

> levels(PctCtr$Index) 
[1] "AAPL" "AIG" "AMZN" "C"  "COST" "ICE" "KO" "MMM" "Total" 
[10] "VTR" "WMT" 

請幫我解決這個問題。謝謝。

+0

請幫助我瞭解爲什麼這個問題是downvoted多次前更改索引列的水平。 –

+0

因爲這個問題沒有清楚地解釋索引應該是什麼,所以我們必須猜測它是索引列中列出的那個,如果完全運行腳本也不起作用。你測試過了嗎? – Al14

+0

你是什麼意思不解釋應該是什麼? – AK88

回答

0

你可以因素和情節

PctCtr$Index <- factor(PctCtr$Index, levels=unique(PctCtr[,"Index"])) 
相關問題