Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to align the title of a ggplot to the right?

Tags:

I am generating a figure that will be used as a column of labels to the right of a three-panel figure, and I would like the title of the figure to right-align as do the labels in the figure itself.

here is a minimal example in which I would like to right-align the title 'words'.

ggplot() + 
  geom_text(aes(y = 1, x = seq(4), 
                label = c('fee', 'fi', 'fo', 'fum'), 
                hjust = 1)) +      
  opts(title = 'words') + 
  coord_flip() + 
  scale_y_continuous(breaks = c(0,0), limits = c(0,1)) 

Which produces this:

enter image description here

update

The answer by @joran is helpful, but it does not align the words with the labels. changing his code from hjust=1 to hjust = 0.96 gets close, but this is more of a hack than a satisfying answer.

like image 628
David LeBauer Avatar asked Jun 29 '11 17:06

David LeBauer


People also ask

How do I change the size of my Ggtitle?

To change the size of the title and subtitle, we add the theme() function to labs() or ggtitle() function, whatever you used. Here we use labs() function. Inside theme() function, we use plot. title parameter for doing changes in the title of plot and plot.


1 Answers

You can do that with the following:

opts(plot.title = theme_text(hjust=1))

More generally, here is a reasonably complete list of things that can be altered via opts and you can see some example code running through some of these options at Hadley's site here, particularly the section on 'polishing'. Even better would be to buy his book.

Note: Since version 0.9.2 opts has been replaced by theme:

theme(plot.title = element_text(hjust = 1)) 
like image 109
joran Avatar answered Sep 20 '22 07:09

joran