放置标签而不丢失实际图表的空间

Fer*_*and 1 r ggplot2

我想显示一个相对较长的标签,并且不希望这个标签占用图表的空间。

例子:

library(ggplot2)
out <- ggplot(economics, aes(x=date,y=unemploy)) + 
              geom_line() + 
              labs(title="US unemployment rate", 
                   subtitle="(%)",
                   caption="Source: St. Louis Fed.\n Last observation: April 2014.",
                   tag="us_unempl.pdf (last update: 2019-01-15, 22:30)") +
              theme(plot.caption=element_text(hjust=0),
                    plot.tag=element_text(size=rel(1)),
                    plot.tag.position="bottomright")
print(out)
Run Code Online (Sandbox Code Playgroud)

在此示例中,相对较宽的标签会导致实际图表的空间大量损失,因为图表的右边距向左移动。

如何在图表下方显示标签 - 理想情况下与标题第二行相对,或者与标题下方绘图的右边缘对齐?

注意:我需要其他信息的标题(例如在我的示例中),否则在plot.caption中使用 hjust=1 显然是一个自然的解决方案。

PoG*_*bas 8

您可以使用具有 x 和 y 位置 ( ) 的数值向量手动指定标签位置plot.tag.positionc(x, y)应介于 0 和 1 之间。c(0, 0)将标记放置到 \xe2\x80\x9c 左下\xe2\x80\x9d 并将c(1, 1)标记放置到 \xe2\x80\x9c 右上\xe2\x80\x9d。

\n\n
library(ggplot2)\nggplot(mtcars, aes(cyl, mpg)) + \n    geom_line() + \n    labs(title = "US unemployment rate", \n         subtitle = "(%)",\n         caption = "Source: St. Louis Fed.\\n Last observation: April 2014.",\n         tag = "us_unempl.pdf (last update: 2019-01-15, 22:30)") +\n    theme(plot.caption = element_text(hjust = 0),\n          plot.tag = element_text(size = rel(1)),\n          plot.tag.position = c(0.85, 0.05))\n
Run Code Online (Sandbox Code Playgroud)\n\n

在此输入图像描述

\n