Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R plotting an inset plot on just one plot in a multi-plot layout

Tags:

r

layout

I have a series of plots that I want on a single page. I first use the command layout to specify my plot layout:

layout(matrix(c(1,1,2,2,1,1,2,2,3,4,5,6),3,4,byrow=TRUE))

For plot 1, I have something like:

plot(Easting,Northing, pch=16, col=grey(cex.size)) #The cex.size colours my dots according to some value

I now want to draw an inset plot on plot 1, but not move to plot 2 yet. I tried following the code :

par(fig=c(0.75, 1, 0, 0.25), new = T)
plot(spp.tmp[,1:2], col=cols[spp.tmp[,3]+1], pch=16)
par(fig=c(0,1,0,1))

But this doesnt work, as par(fig()) command overwrites my layout, and the inset plot appears on the bottom corner of my overall figure, not just in the bottom corner of plot 1.

like image 846
amg Avatar asked Mar 21 '13 03:03

amg


People also ask

How do I arrange multiple plots in R?

To arrange multiple ggplot2 graphs on the same page, the standard R functions - par() and layout() - cannot be used. The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page.

What does PAR () do in R?

The par() function is used to set or query graphical parameters. We can divide the frame into the desired grid, add a margin to the plot or change the background color of the frame by using the par() function. We can use the par() function in R to create multiple plots at once.


2 Answers

Two options,

You coul try and include the inset within your layout command (if you were to stick with layout

Here is a case where the first plot spans two rows and column, the second is an inset in the bottom right corner of the first. The third plot is below, the same size as the first, but without an inset.

layout( matrix(c(1,1,1,2,3,3,3,3), 4, 2, byrow = TRUE) )
## show the regions that have been allocated to each plot
layout.show(3)

enter image description here

An alternative is to use subplot from the TeachingDemos package

library(TeachingDemos)
layout(matrix(c(1,1,0,2),2,2,TRUE))
plot(1)
subplot(plot(1), x = c(1.2),y=0.8)
plot(2)

enter image description here

like image 157
mnel Avatar answered Sep 19 '22 14:09

mnel


This is my hatchet approach using base graphics. Because your messing around with par(), why dont you change around the order in matrix and plot the tricky one last. This way the par settings do not affect any more plots in your layout, if you were to plot the tricky one first. It seems simplistic in this example, but when you have lots of plots and you want an inset in only 1, it works.

##generate some data
x<-rnorm(50)
y<-rnorm(50)
##set the layout
##so your first plot is plotted last
layout(matrix(c(2,2,0,1), 2,2, byrow=T))

#plot 1 is on the bottom right
plot(x,y, col="grey30", xlab="", ylab="")
#plot 2 is across the top
plot(x,y, col="grey30", xlab="", ylab="")
##set par to place the next plot in the existing plotting area
## and use fig to position it
par(fig=c(.65, .95, .55, .85), new = TRUE)

#inset 3rd plot int top plot, this effectively gives you a blank plot to populate
plot(x,y, col="white",  xlab="", ylab="")
#and make the background white
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "white")
##then just add your points afterwards
points(x,y,col="tomato")

like image 27
atlantach_james Avatar answered Sep 21 '22 14:09

atlantach_james