R 如何将标题与换行符左对齐?

R 如何将标题与换行符左对齐?,r,ggplot2,R,Ggplot2,我正在用ggplot2制作一张图表,我想通过左对齐来节省图表libebraking标题中的空间。问题是,使用hjust不正确 library(ggplot2) chart <- ggplot( data = cars, aes( x = speed, y = dist ) ) + geom_point() + labs( title = "Here is a very long title that will need a\nlinebreak

我正在用ggplot2制作一张图表,我想通过左对齐来节省图表libebraking标题中的空间。问题是,使用
hjust
不正确

library(ggplot2)

chart <- ggplot(
  data = cars,
  aes(
    x = speed,
    y = dist
  )
) +
  geom_point() +
  labs(
    title = "Here is a very long title that will need a\nlinebreak here",
    subtitle = "This subtitle will also have\na linebreak"
  ) +
  theme(
    plot.title = element_text(
      hjust = -0.1
    )
  )
chart

ggsave(
  filename = "~/Desktop/myplot.png",
  plot = chart,
  # type = "cairo",
  height = 4,
  width = 6,
  dpi = 150)
库(ggplot2)

图表您可以将
geom_text
coord_cartesian(clip=“off”)
一起使用,这允许在绘图面板之外绘制绘图元素

library(ggplot2)

ggplot(
  data = cars,
  aes(x = speed,
      y = dist)) +
  geom_point() +
  labs(subtitle = "This subtitle will also have\na linebreak") +
  geom_text(
    x = 1,
    y = 160,
    inherit.aes = FALSE,
    label = "Here is a very long title that will need a\nlinebreak here",
    check_overlap = TRUE,
    hjust = 0,
    size = 6
  ) +
  coord_cartesian(clip = "off") +
  theme(plot.margin = unit(c(4, 1, 1, 1), "lines"))

另一种方法是使用包中的
ggarrange
,该包具有可用于标题的
top
参数

图表加载所需包:gridExtra
图,
ncol=1,
top=textGrob(
“此处有一个很长的标题,需要在此处\n换行”,
gp=gpar(fontface=1,fontsize=14),
hjust=0,
x=0.01)
)

于2018年9月18日由(v0.2.1.9000)创建