Created
April 3, 2013 01:08
-
-
Save richfitz/5297666 to your computer and use it in GitHub Desktop.
Multi panel figure with inset plots in base graphics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## OK, this turned out to be more complicated than I would have | |
## thought. I feel that this approach *should* work, but it confuses | |
## R's multi-panel plots: | |
## Smaller margins, plus a top margin | |
par(mar=c(3.1, 3.1, .5, .5), oma=c(0, 0, 1.5, 0), mfrow=c(2, 2)) | |
## Plot dummy data | |
plot(1:10) | |
## New coordinates relative to the plot (20% of the height and width, | |
## inset by 10 % at the bottom and 5% on the right), within the | |
## boundaries of the plot box. | |
p <- c(.75, .95, .10, .25) | |
fig.new <- c(grconvertX(p[1:2], from="npc", to="ndc"), | |
grconvertY(p[3:4], from="npc", to="ndc")) | |
op <- par(fig=fig.new, cex=.5, new=TRUE, mar=rep(0, 4)) | |
## Plot the inset | |
plot(1:10, col="red") | |
## Reset the parameters | |
par(op) | |
## Gah - this replaces the current plot | |
plot(1:10) | |
## Worse - this won't advance things. | |
dev.off() | |
## I tried using layout instead of mfrow, but that didn't help either. | |
## Instead, you'll have to get the inset coordinates on the first pass | |
## and add the insets after: | |
## Smaller margins, plus a top margin | |
par(mar=c(3.1, 3.1, .5, .5), oma=c(0, 0, 1.5, 0), mfrow=c(2, 2)) | |
## Plot dummy data | |
plot(1:10) | |
fig.new.1 <- c(grconvertX(p[1:2], from="npc", to="ndc"), | |
grconvertY(p[3:4], from="npc", to="ndc")) | |
plot(1:10) | |
fig.new.2 <- c(grconvertX(p[1:2], from="npc", to="ndc"), | |
grconvertY(p[3:4], from="npc", to="ndc")) | |
plot(1:10) | |
fig.new.3 <- c(grconvertX(p[1:2], from="npc", to="ndc"), | |
grconvertY(p[3:4], from="npc", to="ndc")) | |
plot(1:10) | |
fig.new.4 <- c(grconvertX(p[1:2], from="npc", to="ndc"), | |
grconvertY(p[3:4], from="npc", to="ndc")) | |
op <- par(fig=fig.new.1, cex=.5, new=TRUE, mar=rep(0, 4)) | |
plot(1:10, col="red") | |
par(op) | |
op <- par(fig=fig.new.2, cex=.5, new=TRUE, mar=rep(0, 4)) | |
plot(1:10, col="purple") | |
par(op) | |
op <- par(fig=fig.new.3, cex=.5, new=TRUE, mar=rep(0, 4)) | |
plot(1:10, col="green") | |
par(op) | |
op <- par(fig=fig.new.4, cex=.5, new=TRUE, mar=rep(0, 4)) | |
plot(1:10, col="blue") | |
par(op) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment