将ggplot标题放在图表的右上角

zac*_*ach 12 r ggplot2

我正在使用theme_minimal()ggplot0.9.3中的优秀发现,它具有白色背景.我想把我的情节标题放在情节右上角的自定义位置.在下面的示例中,我知道xy值,但我想知道是否有传递方法xmaxymax值以确保文本放置在右上角.理想情况下,文本是正确的.

#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)
Run Code Online (Sandbox Code Playgroud)

Sté*_*ent 33

你可以做

p + annotate("text",  x=Inf, y = Inf, label = "Some text", vjust=1, hjust=1)
Run Code Online (Sandbox Code Playgroud)


Did*_*rts 15

您可以使用功能max()min()内部annotate(),然后添加hjust=1,以确保文本放在里面的情节(合理的).

p + annotate("text", y= max(mtcars$wt), x =max(mtcars$mpg),label="Custom Title",hjust=1) 
Run Code Online (Sandbox Code Playgroud)