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

时间:2014-08-20 09:22:20

标签: r ggplot2

我想在这样的情节中保持标题的对齐

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

basic ggplot2 plot

首次尝试

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)))

enter image description here

你的成功!但是等等......还有更多......现在我想将标题更改为其他内容。

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))

new title

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

问题

如何在不弄乱hjust值的情况下,一次又一次地使标题左对齐(y轴标签左边的几个像素)?或者hjust和字符串长度之间的关系是什么?

我已尝试根据this question手动注释,但之后我只获得了标题,而且由于某种原因没有其他内容 - 而且是错误。

谢谢!

6 个答案:

答案 0 :(得分:24)

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

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)

enter image description here

答案 1 :(得分:9)

您可以手动调整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]")

我们现在可以将ggplot对象转换为gtable对象,并检查绘图中元素的布局。请注意,title位于网格的第四列,与主面板相同的列。

g <- ggplotGrob(p)
g$layout
#    t l  b r  z clip       name
# 17 1 1 10 7  0   on background
# 1  5 3  5 3  5  off     spacer
# 2  6 3  6 3  7  off     axis-l
# 3  7 3  7 3  3  off     spacer
# 4  5 4  5 4  6  off     axis-t
# 5  6 4  6 4  1   on      panel
# 6  7 4  7 4  9  off     axis-b
# 7  5 5  5 5  4  off     spacer
# 8  6 5  6 5  8  off     axis-r
# 9  7 5  7 5  2  off     spacer
# 10 4 4  4 4 10  off     xlab-t
# 11 8 4  8 4 11  off     xlab-b
# 12 6 2  6 2 12  off     ylab-l
# 13 6 6  6 6 13  off     ylab-r
# 14 3 4  3 4 14  off   subtitle
# 15 2 4  2 4 15  off      title
# 16 9 4  9 4 16  off    caption

要将title与绘图的左边缘对齐,我们可以将l值更改为1.

g$layout$l[g$layout$name == "title"] <- 1

绘制修改过的网格:

grid::grid.draw(g)

结果: enter image description here

答案 2 :(得分:7)

另一种方法是使用theme()。使用labs功能为x轴标记所有标题x =,为y轴标记y =,为标题标题标记title =fill =或{{1}如果你有一个传奇,你想要一个标题。在colour =theme(),这将使左侧的情节标题合理化。您可以删除hjust = 0,图标题将居中对齐。

hjust = 0

答案 3 :(得分:6)

我专门在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()

enter image description here

答案 4 :(得分:5)

此问题涉及github tidyverse / ggplot2解决的问题:https://github.com/tidyverse/ggplot2/issues/3252

它在ggplot2(开发版本)中实现:https://github.com/tidyverse/ggplot2/blob/15263f7580d6b5100989f7c1da5d2f5255e480f9/NEWS.md

  

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

以您的示例作为代表:

# First install the development version from GitHub:
#install.packages("devtools") #If required
#devtools::install_github("tidyverse/ggplot2")

library(ggplot2)
packageVersion("ggplot2")
#> [1] '3.2.1.9000'

ggplot(data = economics, aes(x = date, y = unemploy)) + 
  geom_line() +
  labs(x=NULL,
       y="Unemployed [thousands]",
       title = "Unemployment in USA for some years",
       subtitle = "A subtitle possibly",
       caption  = "NOTE: Maybe a caption too in italics.") +
  theme(plot.caption = element_text(hjust = 0, face= "italic"), #Default is hjust=1
        plot.title.position = "plot", #NEW parameter. Apply for subtitle too.
        plot.caption.position =  "plot") #NEW parameter

reprex package(v0.3.0)于2019-09-04创建

答案 5 :(得分:1)

自 ggplot 3.3.0 发布后,您还可以使用 plot.title.position = "plot" 将标题和 plot.caption.position = "plot 副标题定位在完整情节的左侧。

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")

enter image description here