help grinsethttps://github.com/benjann/grinset/

Title

grinset -- Add a graph as an inset to another graph

Syntax

Syntax 1: add inset

grinset [name] [position] [,
options ] : graph_command

Syntax 2: modify inset

grinset [name] [position] [,
options ] [: id ]

where name is the name of the graph to be modified (default is
the current/topmost graph).

options

size(# [#]) size of the inset; default is size(25)

scale(#) scale of markers and labels; default is scale(0.5)

name(newname[, replace]) custom name for the resulting graph

nodraw do not draw the resulting graph

Description

grinset adds the graph resulting from graph_command as an inset to
the current (topmost) graph (or to the graph identified by name). The command
has been inspired by a note on inset plots by Tibbles and Melse (2023).

Argument position determines the position of the inset on the graph. The
syntax of position is

pos [pos]

where pos may be

# vertical (horizontal) offset from center; positive values
shift up (right), negative values shift down (left)

t=# gap between top of inset and top of graph

b=# gap between bottom of inset and bottom of graph

l=# gap between left edge of inset and left edge of graph

r=# gap between right edge of inset and right edge of graph

At most one of t and b and at most one of l and r
is allowed. In any case, # is interpreted as a
percentage of the minimum of the width and height of the graph. By default,
the inset will be placed at the center of the graph; this is equivalent to
specifying position as 0 0. If position is specified as
# #, the first number is interpreted as vertical offset and the second
as horizontal offset. Alternatively, use declarations t or b and
l or r to position the inset with respect to the borders of the
graph. For example, type

t=5 r=5

to place the inset in the top right corner of the graph, with a margin equivalent
to 5% of the minimum of the width and height of the graph.

In syntax 2, argument id selects the inset to be modified. This is useful
if multiple insets have been added to the graph. Counting is backwards. The
id of the inset added last is 1; this is the
inset that will be modified if id is omitted. The id of the inset
added second last is 2, the id of the inset added third last is
3, and so on. For example, after adding three insets, type

grinset ... : 3

to modify the inset that has been added first (the third last inset).

Options

size(# [#]) sets the height and width of the inset as a percentage of the
height and width of the main graph. The default for a new inset
is size(25). Specify two values to set height and width individually.

When changing the size of an existing inset (Syntax 2), the center of the inset
will remain in the same position. Depending on context, you may thus also want
to specify position.

Changing height and width of the graph after the inset has been added may have
unexpected effects on the inset. It is best not to change the size of the graph
after having added an inset.

scale(#) specifies a multiplier that affects the size
of text, markers, and line widths of the inset. The default for a new inset is
scale(0.5).

name(newname[, replace]) specifies a custom
name for the resulting graph. Use this option if you want to create a new
graph rather then modify the existing graph.

nodraw specifies that the graph not be displayed.

Examples

Main graph:

. sysuse auto, clear
. scatter price mpg

Add a histogram:

. grinset r=5: histogram mpg, percent ylabel(, nogrid)

Move the inset to a different position, change size, and change scaling of text and markers:

. grinset t=5 r=5, size(20) scale(0.4)

Add a second histogram:

. grinset t=5 20, size(20) scale(0.4): histogram price, percent ylabel(, nogrid)

References

Tibbles, Matthew, Eric Melse. 2023. A note on creating inset plots using
graph twoway. The Stata Journal 23(1): 265-275. https://doi.org/10.1177/1536867X231162022

Author

Ben Jann, University of Bern, ben.jann@unibe.ch

Thanks for citing this software as follows:

Jann, B. (2023). grinset: Stata module to add a graph as an inset to
another graph. Available from https://ideas.repec.org/c/boc/bocode/s459203.html.

Also see

Online: help for
graph