11

I have the following graph that I generated using ggplot2 enter image description here

I had finalPlot as the ggplot object. To add labels I used

finalPlot + stat_bin() + scale_x_continuous('Solution Cost') + scale_y_continuous('Number of Solutions')`

How can I change the orientation of the y axis label to make it appear horizontal and if possible span it across two lines like

Number of
Solutions
Ankit
  • 5,827
  • 8
  • 44
  • 82
  • 3
    As far as I can see the answer below is now obsolete since `'opts'` is deprecated (is now `'theme'`) as is `theme_text()` (for `'element_text'`).. – geotheory Oct 28 '13 at 11:55

2 Answers2

19

The syntax has changed in recent versions of ggplot2; if you try the above answer, you'll get

Error: Use 'theme' instead. (Defunct; last used in version 0.9.1)

These days you should use

finalPlot + ylab("Number of\nSolutions") + theme(axis.title.y = element_text(angle=0))
Jeff Hammerbacher
  • 4,081
  • 2
  • 27
  • 35
8

For the rotation angle of the axis text you need to use element_text(). See this post on SO for some examples. For spacing over two lines I would add a "\n" on the location in the string where you want to put the newline.

This will set the correct orientation for the y axis text and force a line break:

finalPlot + ylab("Number of\nSolutions") + 
    theme(axis.title.y = element_text(angle = 0))
PatrickT
  • 8,058
  • 7
  • 59
  • 94
Paul Hiemstra
  • 56,833
  • 11
  • 132
  • 142