Inset small plot within figure

Share:

Description

Inset plot with margins, background and border

Usage

1
2
3
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!

Author(s)

Berry Boessenkool, berry-b@gmx.de, 2014

See Also

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.