I want to loop over a set of plots and each of them have another plot inside the plot using base R.
Following the example created in this previous question and as an example an individual plot could look something like this:
plotDF <- data.frame (x = rep(1:5, 4), y = rep(1:5, 4),
ID = rep(c("model1", "model2", "model3", "model4"),
each = 5))
plotDF2 <- plotDF[plotDF$ID == unique(plotDF$ID)[1],]
plot(plotDF2$x, plotDF2$y, main = unique(plotDF2$ID))
## Second plot
par(fig = c(grconvertX(c(1, 3), from="user", to="ndc"),
grconvertY(c(3, 5), from="user", to="ndc")),
mar = c(0,0,0,0),
new = TRUE)
curve(exp(x), from=1, to=5, lwd=7, xlab="chi", ylab="exp(x)", cex.lab=4,axes=F)
axis(1, labels=NA,at=c(0,5))
axis(2, labels=NA,at=c(0,150))
And I want to plot in a loop multiple plots which each of them has two plots, like in this image but with both plots in each plot.
My attempt does not work and I guess it has to do with the par() set up but I could not figure out how to change it to make it work.
plotDF <- data.frame (x = rep(1:5, 4), y = rep(1:5, 4),
ID = rep(c("model1", "model2", "model3", "model4"),
each = 5))
par(mfcol=c(2,2), mar=c(2,2,1,1))
for (i in 1:length(unique(plotDF$ID))){
plotDF2 <- plotDF[plotDF$ID == unique(plotDF$ID)[i],]
plot(plotDF2$x, plotDF2$y, main = unique(plotDF2$ID))
## Second plot
par(fig = c(grconvertX(c(1, 3), from="user", to="ndc"),
grconvertY(c(3, 5), from="user", to="ndc")),
mar = c(0,0,0,0),
new = TRUE)
curve(exp(x), from=1, to=5, lwd=7, xlab="chi", ylab="exp(x)", cex.lab=4,axes=F)
axis(1, labels=NA,at=c(0,5))
axis(2, labels=NA,at=c(0,150))
}