lets_plot.geom_label

lets_plot.geom_label(mapping=None, *, data=None, stat=None, position=None, show_legend=None, sampling=None, tooltips=None, map=None, map_join=None, use_crs=None, label_format=None, na_text=None, nudge_x=None, nudge_y=None, label_padding=None, label_r=None, label_size=None, **other_args)

Add a text directly to the plot with a rectangle behind the text.

Parameters
mappingFeatureSpec

Set of aesthetic mappings created by aes() function. Aesthetic mappings describe the way that variables in the data are mapped to plot “aesthetics”.

datadict or DataFrame or polars.DataFrame or GeoDataFrame

The data to be displayed in this layer. If None, the default, the data is inherited from the plot data as specified in the call to ggplot.

statstr, default=’identity’

The statistical transformation to use on the data for this layer, as a string. Supported transformations: ‘identity’ (leaves the data unchanged), ‘count’ (counts number of points with same x-axis coordinate), ‘bin’ (counts number of points with x-axis coordinate in the same bin), ‘smooth’ (performs smoothing - linear default), ‘density’ (computes and draws kernel density estimate).

positionstr or FeatureSpec

Position adjustment, either as a string (‘identity’, ‘stack’, ‘dodge’, …), or the result of a call to a position adjustment function.

show_legendbool, default=True

False - do not show legend for this layer.

samplingFeatureSpec

Result of the call to the sampling_xxx() function. Value None (or ‘none’) will disable sampling for this layer.

tooltipslayer_tooltips

Result of the call to the layer_tooltips() function. Specifies appearance, style and content.

mapGeoDataFrame or Geocoder

Data containing coordinates of points.

map_joinstr or list

Keys used to join map coordinates with data. First value in pair - column/columns in data. Second value in pair - column/columns in map.

use_crsstr, optional, default=”EPSG:4326” (aka WGS84)

EPSG code of coordinate reference system (CRS). All coordinates in GeoDataFrame (see the ‘map’ parameter) will be projected to this CRS.

label_formatstr

Format used to transform label mapping values to a string. Examples: ‘.2f’ -> ‘12.45’, ‘Num {}’ -> ‘Num 12.456789’, ‘TTL: {.2f}$’ -> ‘TTL: 12.45$’. For more info see https://lets-plot.org/pages/formats.html.

nudge_xfloat

Horizontal adjustment to nudge labels by.

nudge_yfloat

Vertical adjustment to nudge labels by.

na_textstr, default=’n/a’

Text to show for missing values.

label_paddingfloat

Amount of padding around label. Defaults to 0.25 of font size.

label_rfloat

Radius of rounded corners. Defaults to 0.15 of label height.

label_sizefloat, default = 1.0

Size of label border.

other_args

Other arguments passed on to the layer. These are often aesthetics settings used to set an aesthetic to a fixed value, like color=’red’, fill=’blue’, size=3 or shape=21. They may also be parameters to the paired geom/stat.

Returns
LayerSpec

Geom object specification.

Notes

Adds a text directly to the plot and draws a rectangle behind the text, making it easier to read.

geom_label() understands the following aesthetics mappings:

  • x : x-axis value.

  • y : y-axis value.

  • alpha : transparency level of a layer. Accepts values between 0 and 1.

  • color (colour) : color of a geometry. Can be continuous or discrete. For continuous value this will be a color gradient between two colors.

  • fill: background color of the label.

  • size : font size.

  • label : text to add to plot.

  • family : font family. Possible values: ‘sans’, ‘serif’, ‘mono’, any other like: “Times New Roman”. The default is ‘sans’.

  • fontface : font style and weight. Possible values: ‘plain’, ‘bold’, ‘italic’, ‘bold italic’. The default is ‘plain’.

  • hjusthorizontal alignment. Possible values: ‘left’, ‘middle’, ‘right’ or number between 0 (‘left’) and 1 (‘right’).

    There are two special alignments: ‘inward’ (aligns label towards the plot center) and ‘outward’ (away from the plot center).

  • vjustvertical alignment. Possible values: ‘bottom’, ‘center’, ‘top’ or number between 0 (‘bottom’) and 1 (‘top’).

    There are two special alignments: ‘inward’ (aligns label towards the plot center) and ‘outward’ (away from the plot center).

  • angle : rotation angle in degrees.

  • lineheight : line height multiplier applied to the font size in the case of multi-line text.


The data and map parameters of GeoDataFrame type support shapes Point and MultiPoint.

The map parameter of Geocoder type implicitly invoke centroids() function.


The conventions for the values of map_join parameter are as follows.

  • Joining data and GeoDataFrame object

    Data has a column named ‘State_name’ and GeoDataFrame has a matching column named ‘state’:

    • map_join=[‘State_Name’, ‘state’]

    • map_join=[[‘State_Name’], [‘state’]]

  • Joining data and Geocoder object

    Data has a column named ‘State_name’. The matching key in Geocoder is always ‘state’ (providing it is a state-level geocoder) and can be omitted:

    • map_join=’State_Name’

    • map_join=[‘State_Name’]

  • Joining data by composite key

    Joining by composite key works like in examples above, but instead of using a string for a simple key you need to use an array of strings for a composite key. The names in the composite key must be in the same order as in the US street addresses convention: ‘city’, ‘county’, ‘state’, ‘country’. For example, the data has columns ‘State_name’ and ‘County_name’. Joining with a 2-keys county level Geocoder object (the Geocoder keys ‘county’ and ‘state’ are omitted in this case):

    • map_join=[‘County_name’, ‘State_Name’]

Examples

1from lets_plot import *
2LetsPlot.setup_html()
3ggplot() + geom_label(x=0, y=0, label='Lorem ipsum', size=14)