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()
highlight_axis_labels = theme(
text=element_text(color='gray', size=13),
axis_text=element_text(color='blue')
)
p = ggplot(df) + geom_jitter(aes("state", "poptotal"), color='light_gray', seed=42)
p
Let's change rotation angle to check placement of labels on a discrete axis. Labels on y-axis are removed for this demo.
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°")
])
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°")
])
For continuous axis the number of labels varies depending on how many it can fit.
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(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°")
])
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°")
])