Learn R Programming

berryFunctions (version 1.11.0)

smallPlot: Inset small plot within figure

Description

Inset plot with margins, background and border

Usage

smallPlot(expr, x = c(5, 70), y = c(50, 100), x1, y1, x2, y2, mar = c(12, 14, 3, 3), mgp = c(1.8, 0.8, 0), bg = par("bg"), border = par("fg"), las = 1, resetfocus = TRUE, colwise = FALSE, ...)

Arguments

expr
expression creating a plot. Can be code within braces.
x, y
Position of small plot, relative to current figure region (0:100). max and min from vector are taken. DEFAULT: 5-70, 50-100
x1, y1, x2, y2
Positions of topleft and bottomright corner. If any is missing, it is taken from x or y
mar
Margin vector in relative units (0:100), thus behaves differently than par(mar). DEFAULT: c(12, 14, 3, 3)
mgp
MarGinPlacement: distance of xlab/ylab, numbers and line from plot margin, as in par, but with different defaults. DEFAULT: c(1.8, 0.8, 0)
bg
Background. DEFAULT: par("bg")
border
Border around inset plot. DEFAULT: par("fg")
las
LabelAxisStyle. DEFAULT: 1
resetfocus
Reset focus to original plot? Specifies where further low level plot commands are directed to. DEFAULT: TRUE
colwise
Logical: Continue next plot below current plot? If you had par(mfcol=...), you must use colwise=TRUE, otherwise the next plot will be to the right of the current plot (as with par(mfrow=...)). DEFAULT: FALSE
...
further arguments passed to par. new=F removes old plot. May mess things up - please tell me for which arguments!

Value

parameters of small plot, invisible.

Warning

setting mai etc does not work!

See Also

colPointsHist for an example of usage, subplot and add.scatter for alternative solutions to this problem that do not set margins.

Examples

Run this code

# Basic usage:
op <- par(no.readonly=TRUE) # original parameters
plot(1:10)
smallPlot(plot(5:1) )
smallPlot(plot(5:1), x=c(30,80), y=30:60, bg="yellow", yaxt="n")
# if R warns "figure margins too large", try dragging the plot viewer bigger

# select focus for further add-on's:
points(2, 2, pch="+", cex=2, col=2) # main window
smallPlot( plot(5:1), bg="lightblue", resetfocus=FALSE )
points(2, 2, pch="+", cex=2, col=2) # smallPlot window
par(op)

# More par settings:
plot(1:10)
smallPlot( plot(50:1), bg=6, mai=c(0.2, 0.3, 0.1, 0.1)) # screws up
smallPlot( plot(5:1), bg=8, ann=FALSE)
smallPlot(plot(10:50), bg="transparent") # old plot is kept
smallPlot(plot(10:50))

# complex graphics in code chunks:
plot(1:10)
smallPlot( {plot(5:1, ylab="Blubber"); lines(c(2,4,3));
            legend("topright", "BerryRocks!", lwd=3)    }, bg="white" )


# multiple figure situations
old_plt <- par("plt")
par(mfcol=c(3,4))
new_plt <- par("plt")
plot(1:10)
plot(1:10)
smallPlot(plot(5:1), bg="lightblue", colwise=TRUE)
points(3, 2, pch="+", cex=2, col=2)
plot(1:10) # canot keep mfcol, only mfrow, if colwise is left FALSE.
smallPlot(plot(5:1), bg="bisque", resetfocus=FALSE )
points(3, 2, pch="+", cex=2, col=2)
plot(1:10) # in smallPlot space
par(plt=old_plt)
plot(1:10) # too large
smallPlot(plot(5:1), bg="palegreen")
points(3, 2, pch="+", cex=2, col=2, xpd=NA) # not drawn with default xpd
par(plt=new_plt)
plot(1:10) # canot keep mfcol, only mfrow, if colwise is left FALSE.
smallPlot(plot(5:1), bg="yellow") 
points(3, 2, pch="+", cex=2, col=2)   # everything back to normal

par(op)
par(mfrow=c(3,4))
plot(1:10)
plot(1:10)
smallPlot(plot(5:1), bg="lightblue", colwise=TRUE)
plot(1:10) 
smallPlot(plot(5:1), bg="bisque")
plot(1:10)

Run the code above in your browser using DataLab