check_overlap¶The check_overlap parameter in geom_text() and geom_label() is used to prevent overlapping text labels in the same layer.
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
mpg = pd.read_csv ("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg.head(3)
p = ggplot(mpg, aes('displ', 'hwy')) + \
theme(legend_position = "none", panel_background=element_rect(fill="#CCCCCC")) + \
ggsize(600, 400)
Without check_overlap: The default behavior plots all labels, which can result in an overcrowded plot.
p + geom_text(aes(label='class', color='class'))
With check_overlap=True: Text labels that overlap existing labels are not rendered.
The labels are processed in the order they appear in the data frame, and if a subsequent
label would overlap with a previous one, it is omitted.
p + geom_text(aes(label='class', color='class'), check_overlap=True)
check_overlap is also applicable for geom_label.
p + geom_label(aes(label='class'), check_overlap=True)