hjust, vjust)¶The hjust and vjust parameters control the horizontal and vertical justification of axis labels in lets-plot.
hjust (Horizontal Justification):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):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.
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)
# 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")
p + ggtitle("90° Rotation, Center-Aligned Vertically") + \
theme(axis_text_x=element_text(angle=90, vjust=0.5))
p + ggtitle("30° Rotation, Aligned to the Ticks") + \
theme(axis_text_x=element_text(angle=30, hjust=1.0, vjust=1.0))
p + ggtitle("40° Rotation, at the Bottom, Center-aligned Horizontally") + \
theme(axis_text_x=element_text(angle=40, hjust=0.5, vjust=0))
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")
cp + theme(axis_text_y=element_text(angle=0, hjust=1))