hjust
, vjust
)¶The hjust
and vjust
parameters control the horizontal and vertical justification of axis labels.
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.
%useLatestDescriptors
%use dataframe
%use lets-plot
val mpg = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg.head(3)
DataFrame: rowsCount = 3, columnsCount = 12
untitled | manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | audi | a4 | 1.800000 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
2 | audi | a4 | 1.800000 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
3 | audi | a4 | 2.000000 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
// Base plot with manufacturer on x-axis
val p = letsPlot(mpg.toMap()) {
x = "manufacturer"
y = "hwy"
} + geomBoxplot(fill = "#7CF", color = "#888") +
xlab("Manufacturer") +
ylab("Highway MPG")
p + ggtitle("Default Tick Labels")
p + ggtitle("90° Rotation, Center-Aligned Vertically") +
theme(axisTextX = elementText(angle = 90, vjust = 0.5))
p + ggtitle("30° Rotation, Aligned to the Ticks") +
theme(axisTextX = elementText(angle = 30, hjust = 1.0, vjust = 1.0))
p + ggtitle("40° Rotation, at the Bottom, Center-aligned Horizontally") +
theme(axisTextX = elementText(angle = 40, hjust = 0.5, vjust = 0.0))
data class CategoryData(val category: String, val value: Int)
val cdata = listOf(
CategoryData("Highly Recommended", 25),
CategoryData("Moderately Satisfactory", 40),
CategoryData("Needs Improvement", 15),
CategoryData("Exceeds Expectations", 35),
CategoryData("Barely Acceptable Quality", 10)
)
val cdataMap = mapOf(
"category" to cdata.map { it.category },
"value" to cdata.map { it.value }
)
val cp = letsPlot(cdataMap) {
x = "value"
y = "category"
} + geomBar(stat = Stat.identity, fill = "#FB7") +
ggtitle("Y-Axis Label Justification Demo") +
xlab("Value") +
ylab("Category")
cp + theme(axisTextY = elementText(hjust = 1.0))