Legend
Legend

Reputation: 116820

Is it possible to split X-Axis labels into two levels?

My X-axis labels are strings and rotating them is making my plot look ugly. In ggplot, is there a way to split up the X-axis labels such that half of them appear on the top and half on the bottom (alternating style). So instead of:

Label1 Label2 Label3 Label4

I want:

Label1   Label3
    Label2    Label4

Upvotes: 1

Views: 998

Answers (1)

joran
joran

Reputation: 173577

You can always prepend the x axis values with alternating newline characters:

dat <- data.frame(x = c('Label1','\nLabel2','Label3','\nLabel4'),y = 1:4)
ggplot(data = dat, aes(x = x, y = y)) + 
  geom_point()

which produces this:

enter image description here

As a side note, I thought that perhaps you could pass a vector of values to vjust in opts, but that didn't seem to work. It might in the development version though.

Upvotes: 2

Related Questions