Saravanan K
Saravanan K

Reputation: 684

Nested Plots in R

Good Morning,

I am trying to create 1 x 3 plot where each of the individual plot itself a combine plots of 3 x 3. The number below represent the individual plot 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

Tried using par, and layout. It is not coming out well as all the 3 plots coming out individually and not nested. Perhaps I have confused myself.

Is there any other way of doing it.

Minimal Dataset (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 Script,

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)

Unfortunately the above produces, 3 different, 3x3 plots as shown below, whereas all the 27 plots need to be position as the map given in the first paragraph.

Plot 1 - Plot 9 Plot 1 - Plot 9

Plot 10 - Plot 18 Plot 10 - Plot 18

Plot 19 - Plot 27 Plot 19 - Plot 27

Appreciate if you could give me a headup on this.

Upvotes: 0

Views: 2417

Answers (1)

Peter Lustig
Peter Lustig

Reputation: 953

http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/ gives a nice example on how to arrange multiple plots using the multiplot(p1, p2, p3, p4, cols=2) command.

Note: I am not familiar with the leveragePlots command that apparently is included in the car library.

In your case: Each individual plot p1, p2 and p3 can then be 'built' using something like this:

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

and finally arranged with the multiplot command. (you need to make the multiplot command available by using the code at the bottom of the page that I linked to)

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

Upvotes: 2

Related Questions