我需要在R中绘制4个图,其中x轴上的位置和y轴上的弹珠数。数据中有四个组,根据Set
和Block
是唯一的。
现在,我根据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 :(得分:1)
您可以使用ggplot2::facet_wrap()
:
library(ggplot2)
ggplot(data = dat, aes(x = Position, y = Marbles)) +
geom_point() + facet_wrap(~Block)
<强> 数据:的强>
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")
答案 1 :(得分:0)
实际上使用par
和基础plot
。
op <- par(mfrow=c(2, 2))
by(d, d$Block, function(b) plot(Marbles ~ Position, b, ylim=c(0, max(d$Marbles)),
main=unique(b$Block)))
par(op)
数据
d <- structure(list(Set = c(1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 6L,
6L, 6L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L), Size = c("Small", "Small",
"Small", "Small", "Small", "Small", "Small", "Small", "Small",
"Small", "Small", "Small", "Small", "Small", "Small", "Small",
"Small", "Small", "Small", "Small"), Position = c(1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L), Marbles = c(8L, 81L, 3L, 4L, 8L, 81L, 3L, 4L, 14L, 11L,
12L, 25L, 8L, 81L, 3L, 4L, 14L, 11L, 12L, 25L), Block = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L)), class = "data.frame", row.names = c(NA, -20L))