geom_fit_text() shrinks, grows and wraps text to fit inside a defined box. geom_bar_text() is a convenience wrapper around geom_fit_text() for labelling bar plots generated with geom_col() and geom_bar().

geom_bar_text(mapping = NULL, data = NULL, stat = "identity",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, padding.x = grid::unit(1, "mm"),
padding.y = grid::unit(1, "mm"), min.size = 8, place = "top",
grow = FALSE, reflow = FALSE, hjust = NULL, vjust = NULL,
fullheight = NULL, width = NULL, height = NULL, formatter = NULL,
contrast = FALSE, outside = TRUE, ...)

geom_fit_text(mapping = NULL, data = NULL, stat = "identity",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, padding.x = grid::unit(1, "mm"),
padding.y = grid::unit(1, "mm"), min.size = 4, place = "centre",
outside = FALSE, grow = FALSE, reflow = FALSE, hjust = NULL,
vjust = NULL, fullheight = NULL, width = NULL, height = NULL,
formatter = NULL, contrast = FALSE, ...)

## Arguments

mapping ggplot2::aes() object as standard in 'ggplot2'. Note that aesthetics specifying the box must be provided. See Details. Standard geom arguments as for ggplot2::geom_text(). Horizontal and vertical padding around the text, expressed in grid::unit() objects. Both default to 1 mm. Minimum font size, in points. Text that would need to be shrunk below this size to fit the box will be hidden. Defaults to 4 pt. Where inside the box to place the text. Default is 'centre'; other options are 'topleft', 'top', 'topright', 'right', 'bottomright', 'bottom', 'bottomleft', 'left', and 'center'/'middle' which are both synonyms for 'centre'. If TRUE, text will be grown as well as shrunk to fill the box. Defaults to FALSE. If TRUE, text will be reflowed (wrapped) to better fit the box. Defaults to FALSE. Horizontal and vertical justification of the text. By default, these are automatically set to appropriate values based on place. If TRUE, descenders will be counted when resizing and placing text; if FALSE, only the x-height and ascenders will be counted. The main use for this option is for aligning text at the baseline (FALSE) or preventing descenders from spilling outside the box (TRUE). By default this is set automatically depending on place and grow. When using x and/or y aesthetics, these set the width and/or height of the box. These should be either grid::unit() objects or numeric values on the x and y scales. A function that will be applied to the text before it is drawn. This is useful when using geom_fit_text() in context involving interpolated variables, such as with the 'gganimate' package. formatter will be applied serially to each element in the label column, so it does not need to be a vectorised function. If TRUE and in combination with a fill aesthetic, the colour of the text will be inverted for better contrast against dark background fills. FALSE by default. If TRUE, text placed in one of 'top', 'right', 'bottom' or 'left' that would need to be shrunk smaller than min.size to fit the box will be drawn outside the box if possible. This is mostly useful for drawing text inside bar/column geoms. Defaults to FALSE.

## Details

Except where noted, geom_fit_text() behaves more or less like ggplot2::geom_text().

There are three ways to define the box in which you want the text to be drawn. The extents of the box on the x and y axes are independent, so any combination of these methods can be used:

1. If the x and/or y aesthetics are used to set the location of the box, the width or height will be set automatically based on the number of discrete values in x and/and y.

2. Alternatively, if x and/or y aesthetics are used, the width and/or height of the box can be overridden with a 'width' and/or 'height' argument. These should be grid::unit() objects; if not, they will be assumed to use the native axis scale.

3. The boundaries of the box can be set using the aesthetics 'xmin' and 'xmax', and/or 'ymin' and 'ymax'.

If the text is too big for the box, it will be shrunk to fit the box. With grow = TRUE, the text will be made to fill the box completely whether that requires shrinking or growing.

reflow = TRUE will cause the text to be reflowed (wrapped) to better fit in the box. If the text cannot be made to fit by reflowing alone, it will be reflowed then shrunk to fit the box. Existing line breaks in the text will be respected when reflowing.

## Aesthetics

• label (required)

• (xmin AND xmax) OR x (required)

• (ymin AND ymax) OR y (required)

• alpha

• angle

• colour

• family

• fontface

• lineheight

• size

## Examples


ggplot2::ggplot(ggplot2::presidential, ggplot2::aes(ymin = start, ymax = end,
label = name, x = party)) +
geom_fit_text(grow = TRUE)