Plot inside the plot

I have a schedule 1

curve(exp(x), from=1, to=5, lwd=5) curve(150-exp(x), from=1, to=5, lwd=5, col="darkblue",add=T) 

and inside it I would like to add the following graph 2

 par(mar=c(7,7,1,1)) 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)) text(1,120,"Alpha",adj=c(0,0),cex=3) text(3.5,10,"Beta",adj=c(0,0),cex=3) 

To get the following

Combined

I would also like to make plot 2 transparent so that if there are some elements of plot 1 behind plot 2, they will still be displayed (just like the blue line). Larger labels of graph 2 and the absence of labels and ticks in its axes are also important.

Is it possible? Please use only basic R solutions (without ggplot2 / no grating)

+8
r graph plot
source share
2 answers

OK, here is an example that I wrote in a 10 by 10 inch format. (Part of what upsets the use of par(fig = ) et al. Is that their effects are very dependent on the size of the plotter.)


Edited to add some clarification:

The base graphic parameter par("fig") describes / sets the location of the figure area in proportion to the "drawing area" (which is usually the entire device for single-line graphics). It takes a vector <4> of the form c(xmin, xmax, ymin, ymax) , consisting of numbers (proportions) between 0 and 1 .

Here I use grconvertX() and grconvertY() to convert xy locations expressed in terms of a larger coordinate system (aka "user" ) to the "ndc" coordinate system (normalized device coordinates). The coordinate system "user" more convenient for the user, and "ndc" (with the above caveats) is the coordinate system used by par("fig") . The grconvert*() call is intended to perform a translation between them.

 ## pdf("fig-in-fig.pdf", width=10, height=10) curve(exp(x), from=1, to=5, lwd=5) curve(150-exp(x), from=1, to=5, lwd=5, col="darkblue",add=T) ## Here the bit I added. par(fig = c(grconvertX(c(1, 3), from="user", to="ndc"), grconvertY(c(50, 125), from="user", to="ndc")), mar = c(4,6,1,1), 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)) text(1,120,"Alpha",adj=c(0,0),cex=3) text(3.5,10,"Beta",adj=c(0,0),cex=3) ## dev.off() 

enter image description here

+15
source share

Here is one approach:

 curve(exp(x), from=1, to=5, lwd=5) curve(150-exp(x), from=1, to=5, lwd=5, col="darkblue",add=T) par(new=TRUE) par(oma=c(1,4,5,1)) par(mfcol=c(2,2), mfg=c(1,1)) par(mar=c(7,7,1,1)) curve(exp(x), from=1, to=5, lwd=7, xlab="chi", ylab="exp(x)", cex.lab=2,axes=F) axis(1, labels=NA,at=c(0,5)) axis(2, labels=NA,at=c(0,150)) text(1,120,"Alpha",adj=c(0,0),cex=1.5) text(4,10,"Beta",adj=c(0,0),cex=1.5) 

Gives me this:

enter image description here

Play with various options (especially oma and mar ) to format the result as you wish.

+7
source share

All Articles