2014-01-28 37 views
1

以下是第一行15我的數據:如何在R中使用多個標準進行繪圖?

> head(df,15) 
    frame.group class lane veh.count mean.speed 
1  [22,319]  2 5   9 23.40345 
2  [22,319]  2 4   9 24.10870 
3  [22,319]  2 1  11 14.70857 
4  [22,319]  2 3   8 20.88783 
5  [22,319]  2 2   6 16.75327 
6 (319,616]  2 5  15 22.21671 
7 (319,616]  2 2  16 23.55468 
8 (319,616]  2 3  12 22.84703 
9 (319,616]  2 4  14 17.55428 
10 (319,616]  2 1  13 16.45327 
11 (319,616]  1 1   1 42.80160 
12 (319,616]  1 2   1 42.34750 
13 (616,913]  2 5  18 30.86468 
14 (319,616]  3 3   2 26.78177 
15 (616,913]  2 4  14 32.34548 

「frame.group」包含的時間間隔,「類」是車輛類即1 =摩托車,2 =汽車,3 =卡車和「車道」包含車道號碼。我想創建3個散點圖,frame.group爲x軸,mean.speed爲y軸,每個類爲1。在一個車輛等級的散點圖中,汽車,我想要5個地塊,即每個車道一個。我嘗試以下:

cars <- subset(df, class==2) 
by(cars, lane, FUN = plot(frame.group, mean.speed)) 

有兩個問題:如預期即5繪製5個不同泳道

1)R不繪製。 2)只有一個被繪製出來,這也可能是因爲我用間隔而不是數字作爲x軸。

我該如何解決上述問題?請幫忙。

回答

0

每次發佈新的繪圖命令時,R會用新繪圖替換現有繪圖。您可以通過執行par(mfrow=c(1,5))來創建一個圖的網格,這將是1行和5個圖(其他數字將具有其他數量的行和列)。如果你想要一個散點圖,而不是一箱線圖,你可以使用plot.default

這是比較容易做到這一切與GGPLOT2庫而不是基礎圖形,並將得到的情節看起來更漂亮:

library(ggplot2) 
ggplot(cars,aes(x=frame.group,y=mean.speed))+geom_point()+facet_wrap(~lane) 

有關更多詳細信息,請參閱ggplot2文檔:http://docs.ggplot2.org/current/

+0

謝謝。到目前爲止,我一直在避免使用ggplot2。但現在是學習它的時候了。你給了我這個動機,謝謝。 –

相關問題