I am making a ggplot. The x-axis are factors, and the labels are long.
I can't shorten the labels, they're as short as they can be.
I am interested to make it so that labels are vertically offset. My preference would be to have every odd label at height 0, and every even at height 2 units more distant from the x-axis.
I have looked here, ggplot-hopeful-help, but have real trouble interpreting what is going on, and so can't make a useful version of this.
Any ideas??
(example code below... I'm not very good at formatting code here, it appears... sry.)
library("ggplot2");
stack <- data.frame(value =rnorm(n = 1000, sd = 2, mean=34)); stack$fact <- as.factor(rep(1:5, each=1000/5));
ggplot(stack, aes(x=fact, y=value)) + geom_boxplot(aes(fill=fact))+ scale_x_discrete(breaks=c("1", "2", "3", "4", "5"), labels=c("hi","don't suggest I shorten the text","I need long labels", "This is a long factor label","This label is very long"))
Since guide_axis
was added in version 3.3.0
ggplot2
now offers an out-of-the-box option to place axis labels on multiple rows using the n.dogde
argument of guide_axis
:
library(ggplot2)
set.seed(123)
stack <- data.frame(value = rnorm(n = 1000, sd = 2, mean = 34))
stack$fact <- as.factor(rep(1:5, each = 1000 / 5))
ggplot(stack, aes(x = fact, y = value)) +
geom_boxplot(aes(fill = fact)) +
scale_x_discrete(
breaks = c("1", "2", "3", "4", "5"),
labels = c(
"hi", "don't suggest I shorten the text", "I need long labels",
"This is a long factor label", "This label is very long"
),
guide = guide_axis(n.dodge = 2)
)