2017-08-24 87 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