R 左调整ggplot2中的标题,或ggtitle的绝对位置

R 左调整ggplot2中的标题,或ggtitle的绝对位置,r,ggplot2,R,Ggplot2,我想在这样一个情节中左对齐标题 ggplot(data = economics, aes(x = date, y = unemploy)) + geom_line() + ggtitle("Unemployment in USA between 1967 and 2007") + xlab("") + ylab("Unemployed [thousands]") 第一次尝试 ggplot(data = economics, aes(x = date, y = unemploy)) +

我想在这样一个情节中左对齐标题

ggplot(data = economics, aes(x = date, y = unemploy)) +
 geom_line() +
 ggtitle("Unemployment in USA between 1967 and 2007") +
 xlab("") +
 ylab("Unemployed [thousands]")

第一次尝试

ggplot(data = economics, aes(x = date, y = unemploy)) + geom_line() +
 ggtitle("Unemployment in USA for some years") +
 xlab("") +
 ylab("Unemployed [thousands]") +
 theme(plot.title = element_text(hjust = -0.45, vjust=2.12)))

太成功了!但是等等。。。还有更多。。。现在我想把标题改成别的

ggplot(data = economics, aes(x = date, y = unemploy)) +
 geom_line() +
 ggtitle("Unemployment in USA between 1967 and 2007") +
 xlab("") +
 ylab("Unemployed [thousands]") +
 theme(plot.title = element_text(hjust = -0.45, vjust=2.12))

所以现在我需要调整hjust…:(

问题

如何使标题一次又一次地左对齐(y轴标签左侧的两个像素左右),而不影响hjust值?或者hjust与字符串长度之间的关系是什么

我曾尝试根据手动注释,但由于某种原因,我只得到了标题,而没有得到任何其他内容,并且出现了一个错误


谢谢!

在有人提出更好的解决方案之前,一种方法是

library(ggplot2)
library(grid)
library(gridExtra)
p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
    geom_line() +
    labs(x = NULL, y = "Unemployed [thousands]", title = NULL)

title.grob <- textGrob(
    label = "Unemployment in USA for some years",
    x = unit(0, "lines"), 
    y = unit(0, "lines"),
    hjust = 0, vjust = 0,
    gp = gpar(fontsize = 16))

p1 <- arrangeGrob(p, top = title.grob)
grid.draw(p1)
库(ggplot2)
图书馆(网格)
图书馆(gridExtra)

p另一种方法是利用
theme()
。使用
labs
函数为x轴标记所有标题
x=
,为y轴标记
y=
,为绘图标题标记
title=
fill=
color=
如果有图例,则要在
theme()内
hjust=0
,这将使绘图标题向左对齐。您可以删除
hjust=0
,绘图标题将居中对齐

labs(x = 'Sex', y = 'Age Mean', title = 'Suicide 2003-2013 Age Mean by Sex') +
  theme(plot.title = element_text(family = 'Helvetica', 
                              color = '#666666', 
                              face = 'bold', 
                              size = 18, 
                              hjust = 0))

您可以手动调整
ggplot
输出的布局。首先,我们设置基本绘图:

library(ggplot2)
p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
 geom_line() +
 labs(title = "Unemployment in USA between 1967 and 2007", 
      x = NULL, y = "Unemployed [thousands]")
要将
标题
与绘图的左边缘对齐,我们可以将
l
值更改为1

g$layout$l[g$layout$name == "title"] <- 1
结果: 我写了
ggdraw()
cowplot
中设置图层,这样我可以在绘图的任何位置轻松地进行注释。它设置了一个坐标系,覆盖整个绘图区域,而不仅仅是绘图面板,并且在x和y方向上从0到1。使用这种方法,可以轻松地将标题放置在任何你想要的位置

library(cowplot)
theme_set(theme_gray()) # revert to ggplot2 default theme

p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
  geom_line() +
  ggtitle("") + # make space for title on the plot
  xlab("") +
  ylab("Unemployed [thousands]")

ggdraw(p) + draw_text("Unemployment in USA between 1967 and 2007", 
                      x = 0.01, y = 0.98, hjust = 0, vjust = 1,
                      size = 12)  # default font size is 14, 
                                  # which is too big for theme_gray()
库(cowplot)
theme_set(theme_gray())#还原为ggplot2默认主题

p此问题指的是github tidyverse/ggplot2解决的问题:

并在ggplot2(开发版本)中实现:

Themes获得了两个新参数plot.title.position和plot.caption.position,可用于自定义相对于整个情节的情节标题/副标题和情节标题的定位方式(@clauswilke,#3252)

要以reprex为例:

#首先从GitHub安装开发版本:
#如果需要,安装.packages(“devtools”)#
#devtools::install_github(“tidyverse/ggplot2”)
图书馆(GG2)
包装版本(“ggplot2”)
#> [1] '3.2.1.9000'
ggplot(数据=经济,aes(x=日期,y=失业))+
geom_线()+
实验室(x=NULL,
y=“失业[千]”,
title=“美国失业数年”,
subtitle=“可能有字幕”,
caption=“注意:可能还有斜体标题。”)+
主题(plot.caption=element_text(hjust=0,face=“italic”),#默认值为hjust=1
plot.title.position=“plot”,#新参数。也申请字幕。
plot.caption.position=“plot”)#新参数


自ggplot 3.3.0发布以来,您还可以使用
plot.title.position=“plot”
将标题和
plot.caption.position=“plot
subtitle放置在完整绘图的左侧

ggplot(data = economics, aes(x = date, y = unemploy)) +
    geom_line() +
    ggtitle("Unemployment in USA between 1967 and 2007") +
    xlab("") +
    ylab("Unemployed [thousands]") +
    theme(plot.title.position = "plot")

谢谢!太好了!如果你没有说“直到有人想出更好的解决方案”,我可能会马上接受这一点。:)谢谢你的快速回复!@konvas我该如何在情节中移动标题,使其位于右上角?对我来说,“hjust”部分有效,但“vjust”部分无效。任何建议都非常感谢!谢谢你能算作更好的解决方案吗?@ClausWilke如何?这与我不必要的新名称(“ggdraw”)完全相同画图“)。此外,在这种情况下,仅仅为了添加一个注释而将整个内容包装成一个不可见的ggplot2似乎有些过分,而且效率低下。@babtiste撇开这是否是一个好方法不谈,我想为
draw*
函数提供一个辩护。它们是围绕
geom.*
的包装不需要数据帧作为输入;(ii)不进行美学映射;(iii)不要继承美学映射。它们对于快速的一次性注释很有用。你可以用Geom做同样的事情,但通常需要至少两倍的代码。可能重复@user3710546-这两个问题不是重复的-这个问题是关于左边框的-另一个问题只需要左对齐。他是尝试与左边框对齐不left Axis非常乐意使用cowplot进行此解决方案!!;)尽管它仅在R studio的Plots部分有效。如果我尝试“缩放”或保存它,绘图将失去对齐。为什么?这是一个非常好的解决方案。我如何将其放入每个图形的图形打印例程中?如何保存此新图形最后一个“正确的情节”@WeihuangWong@canIchangethis看到了。谢谢你,在评论后我自己也弄明白了,忘了删除!谢谢你给我指出了正确的位置!
library(cowplot)
theme_set(theme_gray()) # revert to ggplot2 default theme

p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
  geom_line() +
  ggtitle("") + # make space for title on the plot
  xlab("") +
  ylab("Unemployed [thousands]")

ggdraw(p) + draw_text("Unemployment in USA between 1967 and 2007", 
                      x = 0.01, y = 0.98, hjust = 0, vjust = 1,
                      size = 12)  # default font size is 14, 
                                  # which is too big for theme_gray()
ggplot(data = economics, aes(x = date, y = unemploy)) +
    geom_line() +
    ggtitle("Unemployment in USA between 1967 and 2007") +
    xlab("") +
    ylab("Unemployed [thousands]") +
    theme(plot.title.position = "plot")