Download notebook (.ipynb)

Rotation of Axis Labels#

The angle parameter in element_text() function allows to rotate the text.

Note: currently this only works for axis labels, i.e. for axis_text, axis_text_x, axis_text_y parameters in theme().

import pandas as pd

from lets_plot import *
LetsPlot.setup_html()
LetsPlot.set_theme(theme_light())
df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/midwest.csv')
df["state"] = df["state"].map({
    "IL": "Illinois",
    "IN":" Indiana",
    "MI": "Michigan",
    "OH": "Ohio",
    "WI": "Wisconsin",
})
print(df.shape)
df.head()
(437, 29)
Unnamed: 0 PID county state area poptotal popdensity popwhite popblack popamerindian ... percollege percprof poppovertyknown percpovertyknown percbelowpoverty percchildbelowpovert percadultpoverty percelderlypoverty inmetro category
0 1 561 ADAMS Illinois 0.052 66090 1270.961540 63917 1702 98 ... 19.631392 4.355859 63628 96.274777 13.151443 18.011717 11.009776 12.443812 0 AAR
1 2 562 ALEXANDER Illinois 0.014 10626 759.000000 7054 3496 19 ... 11.243308 2.870315 10529 99.087145 32.244278 45.826514 27.385647 25.228976 0 LHR
2 3 563 BOND Illinois 0.022 14991 681.409091 14477 429 35 ... 17.033819 4.488572 14235 94.956974 12.068844 14.036061 10.852090 12.697410 0 AAR
3 4 564 BOONE Illinois 0.017 30806 1812.117650 29344 127 46 ... 17.278954 4.197800 30337 98.477569 7.209019 11.179536 5.536013 6.217047 1 ALU
4 5 565 BROWN Illinois 0.018 5836 324.222222 5264 547 14 ... 14.475999 3.367680 4815 82.505140 13.520249 13.022889 11.143211 19.200000 0 AAR

5 rows × 29 columns

highlight_axis_labels = theme(
    text=element_text(color='gray', size=13), 
    axis_text=element_text(color='blue')
)

1. Default Axis Labels Layout#

p = ggplot(df) + geom_jitter(aes("state", "poptotal"), color='gray75', seed=42)
p

2. Layout Labels for Discrete Axis#

Let’s change rotation angle to check placement of labels on a discrete axis. Labels on y-axis are removed for this demo.

2.1. Horizontal Axis#

p_x_both = p + theme(axis_text_y=element_blank()) + scale_x_discrete(position="both")

gggrid([
    p_x_both + theme(axis_text_x=element_text(angle=10)) + highlight_axis_labels + ggtitle("10°"),
    p_x_both + theme(axis_text_x=element_text(angle=90)) + highlight_axis_labels + ggtitle("90°")
])

2.2. Vertical Axis#

When the rotation angle is set to 90°, some labels are not displayed to avoid overlapping.

gggrid([
    p_x_both + coord_flip() + theme(axis_text_x=element_text(angle=10)) + highlight_axis_labels + ggtitle("10°"),
    p_x_both + coord_flip() + theme(axis_text_x=element_text(angle=90)) + highlight_axis_labels + ggtitle("90°")
]) + ggsize(1000, 300)

3. Layout Labels for Continuous Axis#

For continuous axis the number of labels varies depending on how many it can fit.

3.1. Horizontal Axis#

Changing the angle of rotation changes the number of labels on the axis: 90° rotation allows to place more labels on the horizontal axis.

p_y_both = p + theme(exponent_format=['e', -8, 8], axis_text_x=element_blank()) + scale_y_continuous(position="both")

gggrid([
    p_y_both + coord_flip() + theme(axis_text=element_text(angle=10)) + highlight_axis_labels + ggtitle("10°"),
    p_y_both + coord_flip() + theme(axis_text=element_text(angle=90)) + highlight_axis_labels + ggtitle("90°")
])

3.2. Vertical Axis#

By changing the angle of rotation, thereby increasing the label heights, we get a reduction in the total number of labels on the vertical axis.

gggrid([
    p_y_both + theme(axis_text=element_text(angle=10)) + highlight_axis_labels + ggtitle("10°"),
    p_y_both + theme(axis_text=element_text(angle=90)) + highlight_axis_labels + ggtitle("90°")
])