2017-08-24 89 views
0

我需要在R中繪製4個圖,其中x軸上的位置和y軸上的彈子數量。數據中有四組,根據SetBlock是唯一的。使用par函數繪製基於數據組的多個圖形

現在,我將根據Block對每個數據進行子設置並繪製它。之後我使用par()函數將它們全部繪製在一起。

有沒有一種簡單的方法繪製數據,而無需手動對它們進行子集劃分?

Set Size Position Marbles Block 
    1 Small 1   8  1 
    1 Small 2   81  1 
    1 Small 3   3  1 
    1 Small 4   4  1 
    4 Small 1   8  1 
    4 Small 2   81  1 
    4 Small 3   3  1 
    4 Small 4   4  1 
    4 Small 1   14  2 
    6 Small 2   11  2 
    6 Small 3   12  2 
    6 Small 4   25  2 
    1 Small 1   8  3 
    1 Small 2   81  3 
    1 Small 3   3  3 
    1 Small 4   4  3 
    6 Small 1   14  4 
    6 Small 2   11  4 
    6 Small 3   12  4 
    6 Small 4   25  4 
+0

如果有人要downvote它,你應該ATLEAST有好感我推理 – Ash

+2

我沒有投票,但你應該包括你的當前代碼,以避免這種情況。 – Masoud

+0

謝謝。我會繼續檢查。 – Ash

回答

1

您可以使用ggplot2::facet_wrap()

library(ggplot2) 
ggplot(data = dat, aes(x = Position, y = Marbles)) + 
     geom_point() + facet_wrap(~Block) 

enter image description here

數據:

dat <- structure(list(Set = c(1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L, 1L, 1L,     
    1L, 1L, 6L, 6L, 6L, 6L), Size = structure(c(1L, 1L, 1L, 1L, 1L,     
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Small", class = "factor"), 
     Position = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,      
     4L, 1L, 2L, 3L, 4L), Marbles = c(8L, 81L, 3L, 4L, 14L, 11L,     
     12L, 25L, 8L, 81L, 3L, 4L, 14L, 11L, 12L, 25L), Block = c(1L,     
     1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L      
     )), .Names = c("Set", "Size", "Position", "Marbles", "Block"     
    ), row.names = c(NA, 16L), class = "data.frame") 
+0

這很有用。但是如果我在我的塊中有多個集合,我需要再次對它進行子集合。無論如何要解決這個問題? – Ash

+0

@Ash'facet_wrap(〜Block + Set)'。 – Masoud

+0

謝謝!這就是訣竅 – Ash