Sometimes you want to draw some text in a ggplot2 plot so that it fits inside a defined area. It’s possible to achieve this by manually fiddling with the font size, but this is both tedious and un-reproducible. ggfittext provides a geom called
geom_fit_text() that automatically resizes text to fit inside a box. It works like this:
geom_text(), the position of the text is set by the
geom_fit_text() automatically infers the width and height of the box in which the text is allowed to fit, and shrinks down any text that is too big.
Another way to make the text fit in the box is by reflowing it; that is, wrapping it over multiple lines. With the
reflow = TRUE argument,
geom_fit_text() will reflow the text before (if still necessary) shrinking it:
If you want the text to be as large as possible, the argument
grow = TRUE will increase the text size to the maximum that will fit in the box. This works well in conjunction with
By default, text is placed in the centre of the box. However, you can place it in a corner or on a side of the box with the
place argument, which takes values “top”, “topright”, “right”, “bottomright”, “bottom”, “bottomleft” and so on:
ggfittext provides a convenience function
geom_bar_text() for labelling bars in bar plots:
geom_bar_text() also works with stacked bar plots (
position = "stack"):
And with dodged bar plots, and
If you want to manually set the edges of the box (instead of having them inferred from
y), you can use
Alternatively, you can set the width and/or height with the
height arguments, which should be
grid::unit() objects. The horizontal and/or vertical centre of the box will be defined by
Text can be drawn in polar coordinates with
geom_fit_text() simply by adding
coord_polar() to the plot. This feature is experimental and currently only in the development version; any bug reports are very welcome.
All arguments to
geom_fit_text() can also be used with
contrastcan be used to automatically invert the colour of the text so it contrasts against a background
padding.ycan be used to set the padding between the text and the edge of the box. By default this is 1 mm. These values must be given as
min.sizesets the minimum font size in points, by default 4 pt. Text smaller than this will be hidden (see also
FALSEby default for
TRUE, text that is placed at “top”, “bottom”, “left” or “right” and must be shrunk smaller than
min.sizeto fit in the box will be flipped to the outside of the box (if it fits there). This is mostly useful for drawing text inside bars in a bar plot, so it is
TRUEby default for
vjustset the horizontal and vertical justification of the text, scaled between 0 (left/bottom) and 1 (right/top). These are both 0.5 by default.
formatterallows you to set a function that will be applied to the text before it is drawn. This is mostly useful in contexts where variables may be interpolated, such as when using gganimate.
fullheightis automatically set depending on place, but can be overridden with this option. This is used to determine the bounding box around the text. If
FALSE, the bounding box includes the x-height of the text and ascenders, but not any descenders. If TRUE, it extends from the top of the ascenders to the bottom of the descenders. This is mostly useful in situations where you want to ensure the baseline of text is consistent between labels (
fullheight = FALSE), or when you want to avoid descenders spilling out of the bounding box (
fullheight = TRUE).