R中的嵌套图

时间:2013-12-28 17:53:41

标签: r

早上好,

我正在尝试创建1 x 3绘图,其中每个绘图本身都是3 x 3的组合图。下面的数字表示单个绘图ID。

1 2 3 <space> 10 11 12 <space> 19 20 21
4 5 6 <space> 13 14 15 <space> 22 23 24
7 8 9 <space> 16 17 18 <space> 25 26 27

尝试使用par和布局。由于所有3个地块都单独出现而没有嵌套,因此效果不佳。也许我弄糊涂了自己。

还有其他办法吗?

最小数据集(sample.txt):

X   Y1  Y2  Y3
277 5.20    3.16    5.92
285 5.17    3.14    5.89
297 4.96    2.97    5.70
308 5.26    3.21    5.97
308 5.11    3.09    5.84
263 5.27    3.22    5.98
278 5.20    3.16    5.92
283 5.16    3.13    5.88
268 5.17    3.14    5.89
250 5.20    3.16    5.92
275 5.18    3.15    5.90
274 5.09    3.07    5.82
312 5.03    3.02    5.77
294 5.21    3.17    5.93
279 5.29    3.24    6.00
300 5.14    3.11    5.86
293 5.09    3.07    5.82
298 5.16    3.13    5.88
290 4.99    2.99    5.73
273 5.23    3.19    5.95
289 5.32    3.26    6.03
279 5.21    3.17    5.93
326 5.14    3.11    5.86
293 5.22    3.18    5.94
256 5.15    3.12    5.87
291 5.09    3.07    5.82
283 5.09    3.07    5.82
284 5.07    3.06    5.80
298 5.27    3.22    5.98
269 5.19    3.15    5.91

R剧本,

library(car)
sampledata <- read.table("H:/sample.txt", header=TRUE)
y.1 <- sampledata$Y1
y.2 <- log(sampledata$Y2)
y.3 <- sqrt(sampledata$Y3)
x.1 <- sampledata$X
x.2 <- (sampledata$X)^2

par(mfrow=c(1,3))

par(mfrow=c(3,3))
leveragePlots(lm(y.1 ~ x.1))
leveragePlots(lm(y.1 ~ x.1))
leveragePlots(lm(y.1 ~ x.1))
leveragePlots(lm(y.1 ~ x.1))
leveragePlots(lm(y.1 ~ x.1))
leveragePlots(lm(y.1 ~ x.1))
leveragePlots(lm(y.1 ~ x.1))
leveragePlots(lm(y.1 ~ x.1 + x.2), layout = NA)
title("Plot 1 - Plot 9", outer=TRUE, line =-2)

par(mfrow=c(3,3))
leveragePlots(lm(y.2 ~ x.1))
leveragePlots(lm(y.2 ~ x.1))
leveragePlots(lm(y.2 ~ x.1))
leveragePlots(lm(y.2 ~ x.1))
leveragePlots(lm(y.2 ~ x.1))
leveragePlots(lm(y.2 ~ x.1))
leveragePlots(lm(y.2 ~ x.1))
leveragePlots(lm(y.2 ~ x.1 + x.2), layout = NA)
title("Plot 10 - Plot 18", outer=TRUE, line =-2)

par(mfrow=c(3,3))
leveragePlots(lm(y.3 ~ x.1))
leveragePlots(lm(y.3 ~ x.1))
leveragePlots(lm(y.3 ~ x.1))
leveragePlots(lm(y.3 ~ x.1))
leveragePlots(lm(y.3 ~ x.1))
leveragePlots(lm(y.3 ~ x.1))
leveragePlots(lm(y.3 ~ x.1))
leveragePlots(lm(y.3 ~ x.1 + x.2), layout = NA)
title("Plot 19 - Plot 27", outer=TRUE, line =-2)

不幸的是,上面产生了3个不同的3x3图,如下所示,而所有27个图需要作为第一段中给出的图的位置。

Plot 1 - Plot 9 情节1 - 情节9

Plot 10 - Plot 18 情节10 - 情节18

Plot 19 - Plot 27 情节19 - 情节27

感谢你能不能给我一个平局。

1 个答案:

答案 0 :(得分:2)

http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/给出了一个很好的例子,说明如何使用多时隙(p1,p2,p3,p4,cols = 2)命令排列多个图。

注意:我不熟悉显然包含在汽车库中的leveragePlots命令。

在你的情况下: 然后可以使用以下内容“构建”每个单独的图p1,p2和p3:

require(ggplot2)

# first subplot
p1 = ggplot(data, aes(x = x, y = y1))
p1 = p1 + geom_point()
p1 = p1 + facet_grid(y2 ~ y3)
p1

# second subplot
p2 = ggplot(data, aes(x = x, y = y2))
p2 = p2 + geom_point()
p2 = p2 + facet_grid(y1 ~ y3)
p2

# third subplot
p3 = ggplot(data, aes(x = x, y = y3))
p3 = p3 + geom_point()
p3 = p3 + facet_grid(y1 ~ y2)
p3

最后用multiplot命令排列。 (您需要使用我链接到的页面底部的代码来使用multiplot命令)

multiplot(p1,p2,p3, cols = 3)