Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position ggplot title at the top right of the plot

Tags:

r

ggplot2

I am using the excellent theme_minimal() found in ggplot0.9.3 which has a white background. I would like to place the title of my plots in a custom location at the top right corner of the plot. In the following example I know the x and y values, but I am wondering if there is a way to pass xmax and ymax values to ensure text placement in the top-right. Ideally, the text would be right justified.

#example plot
p <- qplot(mpg, wt, data = mtcars, colour = cyl)
p +  annotate("text", x = 30, y = 5, label = "Custom Title")

#what I would like
p + annotate("text", y= ymax, x =xmax_or_RightJustified)
like image 945
zach Avatar asked Feb 18 '13 02:02

zach


People also ask

How do I change the position of a title in R?

You can adjust the position of the title with the adj argument, that takes values from 0 (left-justified) to 1 (right-justified). Default value is 0.5. However, if you specify the argument adj inside your plotting function all text will be adjusted. If you only want some texts adjusted use it inside the title function.

How do I change the position of a caption in ggplot2?

position of the theme function allows aliging the caption to the panel ( "panel" , default) or the whole plot (“ plot ”). Note that the default for the caption is right alignment, so you can set hjust = 0 to move the caption to the left of the whole plot.

How do you center the title of a ggplot graph?

title = element_text(hjust = 0.5)) to center the title.


1 Answers

You can do

p + annotate("text",  x=Inf, y = Inf, label = "Some text", vjust=1, hjust=1)
like image 184
Stéphane Laurent Avatar answered Sep 19 '22 17:09

Stéphane Laurent