Is there a way to rotate the x-axis labels in a ggplot plot AND change the theme at the same time?
If I do this, I can rotate the x-axis labels:
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
But if I add a theme, the rotations won't work:
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
theme_minimal()
I tried adding the rotation within the theme_minimal()
function, but that didn't work either.
Thanks.
That's because of the order: theme_minimal
being after theme
overrides the latter. Using
ggplot(ToothGrowth, aes(x = dose, y = len)) +
geom_boxplot() + theme_minimal() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
gives
The theme theme_minimal
changes a lot of things including the changes you made with theme
. So that you should do it in the other way:
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With