2016-01-13 36 views
0

我有下表。我需要通過Freq訂購數據,但是,我還需要顯示兩個x軸標籤 - 一個用於start.station,另一個用於end.station,這樣就可以清楚地知道我正在談論電臺對。我使用interaction,但現在不知道如何輸入代碼以Freq訂購。另外,我想在頂部X軸上顯示start.station,並在底部X軸上顯示end.station。ggplot重新排序w /交互

> new_pairs 
x y Freq start.latittude start.longitude    start.station 
1 359 519 929  40.75188  -73.97770   Pershing\nSquare N 
2 477 465 5032  40.75514  -73.98658  Broadway &\nW 41 St 
3 484 519 1246  40.75188  -73.97770   Pershing\nSquare N 
4 484 318 2654  40.75320  -73.97799 E 43 St &\nVanderbilt\nAve 
5 492 267 1828  40.75098  -73.98765  Broadway &\nW 36 St 
6 492 498 957  40.74855  -73.98808  Broadway &\nW 32 St 
7 492 362 1405  40.75173  -73.98754  Broadway &\nW 37 St 
8 493 477 1582  40.75641  -73.99003   W 41 St &\n8 Ave 
9 493 529 728  40.75757  -73.99099   W 42 St &\n8 Ave 
10 529 2021 1748  40.75929  -73.98860   W 45 St &\n8 Ave 
    end.latitude end.longitude  end.station 
1  40.75510  -73.97499 E 47 St &\nPark Av 
2  40.75641  -73.99003 W 41 St &\n8 Ave 
3  40.75500  -73.98014 W 44 St &\n5 Ave 
4  40.75500  -73.98014 W 44 St &\n5 Ave 
5  40.75020  -73.99093 W 33 St &\n7 Ave 
6  40.75020  -73.99093 W 33 St &\n7 Ave 
7  40.75020  -73.99093 W 33 St &\n7 Ave 
8  40.75680  -73.98291 W 45 St &\n6 Ave 
9  40.75680  -73.98291 W 45 St &\n6 Ave 
10  40.75757  -73.99099 W 42 St &\n8 Ave  


ggplot(data= new_pairs, aes(x= interaction(end.station,start.station) y=Freq)) + 
    geom_bar(stat="identity") + 
    ylab("Bikes received") + 
    xlab("Station") 

enter image description here

+0

你可以創建互動之前,你的情節,像你想 – MLavoie

+0

如何使兩個獨立的X命令你的因子水平 - 在兩端軸? – iskandarblue

+0

我不認爲有2 x軸目前是可能的。有些人已經找到了2 y軸的解決方法,所以也許有人會想出一個解決方案 – MLavoie

回答

1

使用虹膜數據集。我創建一個交互並使用\ n有兩組標籤。你只需要適應scale_x_discrete()表達式爲你的數據集,它應該工作。

iris$Species2 <- iris$Species 
iris$Int <- interaction(iris$Species2, iris$Species) 

ggplot(data= iris, aes(x= Int, y=Petal.Length)) + geom_bar(stat="identity") + scale_x_discrete("Int", labels = c("setosa.setosa" = "setosa\nsetosa","versicolor.versicolor" = "versicolor\nversicolor", 
    "virginica.virginica" = "virginica\nvirginica")) 

enter image description here

+0

美學上,刪除\ n之後的空格(例如'「virginica.virginica」=「virginica \ nvirginica」),這兩條線更好地排列。 – George

+0

@喬治,是的,我發佈後發現它,但感謝評論 – MLavoie