Axis Label Justification (hjust, vjust)

The hjust and vjust parameters control the horizontal and vertical justification of axis labels in lets-plot.

  • hjust (Horizontal Justification):
    Adjusts the horizontal alignment of the label relative to its default position.
    • hjust = 0.0 aligns the label to the left.
    • hjust = 0.5 centers the label.
    • hjust = 1.0 aligns the label to the right.
  • vjust (Vertical Justification):
    Adjusts the vertical alignment of the label relative to its default position.
    • vjust = 0.0 aligns the label to the bottom.
    • vjust = 0.5 centers the label vertically.
    • vjust = 1.0 aligns the label to the top.

Both parameters accept values between 0.0 and 1.0 for precise positioning, allowing for fine-tuned control over label placement in your plots.

In [1]:
from lets_plot import *
import pandas as pd

LetsPlot.setup_html()

# Load the mpg dataset
mpg = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg.head(3)
Out[1]:
Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy fl class
0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
In [2]:
# Base plot with manufacturer on x-axis
p = ggplot(mpg, aes(x=as_discrete('manufacturer'), y='hwy')) + \
    geom_boxplot(fill='#7CF', color='#888') + \
    ggtitle("Manufacturer vs Highway MPG") + \
    xlab("Manufacturer") + ylab("Highway MPG")

p + ggtitle("Default Tick Labels")
Out[2]:
In [3]:
p + ggtitle("90° Rotation, Center-Aligned Vertically") + \
    theme(axis_text_x=element_text(angle=90, vjust=0.5))
Out[3]:
In [4]:
p + ggtitle("30° Rotation, Aligned to the Ticks") + \
    theme(axis_text_x=element_text(angle=30, hjust=1.0, vjust=1.0))
Out[4]:
In [5]:
p + ggtitle("40° Rotation, at the Bottom, Center-aligned Horizontally") + \
    theme(axis_text_x=element_text(angle=40, hjust=0.5, vjust=0))
Out[5]:
In [6]:
cdata = pd.DataFrame({
    'category': [
        'Highly Recommended',
        'Moderately Satisfactory',
        'Needs Improvement',
        'Exceeds Expectations',
        'Barely Acceptable Quality'
    ],
    'value': [25, 40, 15, 35, 10]
})

cp = ggplot(cdata, aes(x='value', y='category')) + \
    geom_bar(stat='identity', fill='#FB7') + \
    ggtitle("Y-Axis Label Justification Demo") + \
    xlab("Value") + ylab("Category")
  • Left-aligning long labels creates a consistent starting edge, improving readability, while aligning with natural left-to-right reading patterns.
In [7]:
cp + theme(axis_text_y=element_text(angle=0, hjust=1))
Out[7]: