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)
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.
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.
title = element_text(hjust = 0.5)) to center the title.
You can do
p + annotate("text", x=Inf, y = Inf, label = "Some text", vjust=1, hjust=1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With