22

I can find options to align the legend and axis labels in ggplot, but not for the tick mark labels.

Is it possible to have those labels not right-aligned to the graph plot-box, but left-aligned flush with either the start of the longest label, or some set distance from the overall graph-border?

Example:

set.seed(1)
library(ggplot2)
axisLabels.x <- c("This is a longer label", 
              "Short label", "Short label","Short label","Short label",
              "This is again a longer label")
labels.wrap  <- lapply(strwrap(axisLabels.x,50,simplify=F),paste,collapse="\n") # word wrap
gg <- data.frame(x=LETTERS[1:6], y=sample(1:10,6))
ggplot(gg) +
  geom_bar(aes(x,y, fill=x), stat="identity")+
  scale_x_discrete(labels=labels.wrap)+
  scale_fill_discrete(guide="none")+
  labs(x="",y="Response")+
  coord_flip()

enter image description here


Wanted:

enter image description here

mhermans
  • 2,097
  • 4
  • 18
  • 31
  • 19
    `theme(axis.text.y = element_text(hjust=0))` – user20650 May 28 '15 at 15:24
  • possible duplicate of [changing font size and direction of axes text in ggplot2](http://stackoverflow.com/questions/13297995/changing-font-size-and-direction-of-axes-text-in-ggplot2) – user20650 May 28 '15 at 15:31

1 Answers1

23

As it provides the solution, I make @user20650's comment an answer.

set.seed(1)
library(ggplot2)
axisLabels.x <- c("This is a longer label", 
                  "Short label", "Short label","Short label","Short label",
                  "This is again a longer label")
labels.wrap  <- lapply(strwrap(axisLabels.x,50,simplify=F),paste,collapse="\n") # word wrap
gg <- data.frame(x=LETTERS[1:6], y=sample(1:10,6))
plot <- ggplot(gg) +
  geom_bar(aes(x,y, fill=x), stat="identity")+
  scale_x_discrete(labels=labels.wrap)+
  scale_fill_discrete(guide="none")+
  labs(x="",y="Response")+
  coord_flip()

And here we go

plot + theme(axis.text.y = element_text(hjust = 0))

left aligned y scale labels

loki
  • 9,816
  • 7
  • 56
  • 82