簡體   English   中英

如何在R中使用多個條件進行繪圖?

[英]How to plot using multiple criteria in R?

以下是我的數據的前15行:

> 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”包含時間間隔,“ class”是車輛類別,即1 =摩托車,2 =汽車,3 =卡車,“ lane”包含車道號。 我想創建3個散點圖,其中frame.group為x軸,mean.speed為y軸,每個類1個。 在一個散點圖上,針對一種車類,例如汽車,我想要5個散點圖,即每個車道一個。 我嘗試了以下操作:

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

有兩個問題:

1)R未按預期繪制,即5條不同車道的5條曲線。 2)僅繪制了一個圖,這也是箱形圖,可能是因為我使用間隔而不是數字作為x軸。

如何解決以上問題? 請幫忙。

每次發出新的繪圖命令時,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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM