我想在这样的情节中保持标题的对齐
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 ...... :(
这个问题
如何在不弄乱hjust值的情况下,一次又一次地使标题左对齐(y轴标签左侧的几个像素)?或者hjust和字符串长度之间的关系是什么?
我试图根据这个问题手动注释,但后来我只获得了标题,而且由于某种原因没有别的 - 并且是一个错误.
谢谢!
小智 48
这个问题是指这个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 包(v0.3.0)于 2019 年 9 月 4 日创建
kon*_*vas 25
在有人提出更好的解决方案之前,一种方法就是这样
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)

Wei*_*ong 12
您可以手动调整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)
另一种方法是利用theme().使用此labs功能可x =为x轴,y =y轴,title =绘图标题标记所有标题,fill =或者colour =如果您有想要放置标题的图例.在里面,这将左侧的情节标题证明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))
我专门编写该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()
小智 5
自 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")
| 归档时间: | 
 | 
| 查看次数: | 26349 次 | 
| 最近记录: |