将ggplot标题放在图的右上角 [英] Position ggplot title at the top right of the plot
问题描述
我使用ggplot0.9.3中的优秀 theme_minimal()
,它有一个白色背景。我想将我的地块的标题放置在地块右上角的自定义位置。在下面的例子中,我知道 x
和 y
值,但我想知道是否有一种方法可以传递 xmax
和 ymax
值确保文本位于右上角。理想情况下,文本将是正确的。
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)
推荐答案
code> max()和 min()
内部 annotate()
,然后添加 hjust = 1
以确保文本放置在plot(对齐)内。
You can use functions max()
and min()
inside annotate()
, and then add hjust=1
to ensure that text is placed inside plot (justified).
p + annotate("text", y= max(mtcars$wt), x =max(mtcars$mpg),label="Custom Title",hjust=1)
这篇关于将ggplot标题放在图的右上角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!