Forked from benmarwick/rotate-axis-labels-ggplot2.R
Created
December 9, 2018 01:01
-
-
Save nathania/364c71a7bc1e6def43708b320ae1bcba to your computer and use it in GitHub Desktop.
I can never remember how to rotate the x-axis labels with ggplot2: theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adapted from https://stackoverflow.com/a/7267364/1036500 by Andrie de Vries | |
library(ggplot2) | |
td <- expand.grid( | |
hjust=c(0, 0.5, 1), | |
vjust=c(0, 0.5, 1), | |
angle=c(0, 45, 90), | |
text="text" | |
) | |
td$angle_exp <- | |
with(td, | |
ifelse(angle == 0, "angle = 0", | |
ifelse(angle == 45, "angle = 45", | |
"angle = 90"))) | |
, | |
ggplot(td, aes(x=hjust, | |
y=vjust)) + | |
geom_point(colour = "red", | |
size = 5) + | |
geom_text(aes(label=text, | |
angle=angle, | |
hjust=hjust, | |
vjust=vjust), | |
size = 5) + | |
facet_grid(~angle_exp) + | |
scale_x_continuous(breaks=c(0, 0.5, 1), | |
expand=c(0, 0.2)) + | |
scale_y_continuous(breaks=c(0, 0.5, 1), | |
expand=c(0, 0.2)) + | |
theme_minimal(base_size = 18) + | |
ggtitle("To rotate x-axis label text 90 degrees we use:\ntheme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))") |
Author
nathania
commented
Dec 9, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment