r - 如何对齐多行标题

标签 r ggplot2 title

这个问题在这里已经有了答案:





Multi-line ggplot Title With Different Font Size, Face, etc [duplicate]

(3 个回答)


8 个月前关闭。




我有问题 ggplot标题定位。当我使用 hjust将标题向右滑动的论点,两行都向左和向右滑动,这是不可取的。我想要它们一个在另一个下面。 drawlabel() , annototations对我的情况没有用,因为它们取决于 x 和 y 轴及其单位(日期、货币、公斤),这意味着每次我必须调整它们时。我想要独特的坐标系,无论我在绘制什么,我都可以轻松地将相同的位置用于不同的绘图。
下面显示了一个不受欢迎的代码示例。

set.seed(10)
df <- data.frame(x=1:10,y=round(rnorm(10)*1:10,2))
ggplot(df,aes(x=x,y=y))+
 geom_line(size=0.75)+
 labs(title = 'Here comes my title like that some words\nand my second line title')+
 theme(
   plot.title.position = 'plot',
   plot.title = element_text(hjust = 0.075)
 )
不良情节
enter image description here
可取的情节
enter image description here

最佳答案

我同意@stefan 在评论中的观点,即似乎无法通过 theme 做到这一点。 .
一种骇人听闻的方法是删除您的 hjust通过编辑网格布局更改和手动移动标题的位置。

library(ggplot)
library(grid)
p <- ggplot(df,aes(x=x,y=y)) +
  geom_line(size=0.75) +
  labs(title = 'Here comes my title like that some words\nand my second line title') +
  theme(plot.title.position = 'plot')
grob <- ggplotGrob(p)
grob$layout[grob$layout$name == "title","l"] <- 4.5
grid.newpage()
grid.draw(grob)
enter image description here
您可以更改 4.5所需的效果。

关于r - 如何对齐多行标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65552555/

相关文章:

r - R中数据从季度到月度的转换

r - 与实际值相比,离散轴刻度和标签发生了偏移

r - ggplot密度图产生意想不到的结果

r - 增加线图中线的最大宽度

javascript - 如何使用 javascript 显示 li 标签的标题

javascript - 使用 ReactJS 动态页面标题

string - 删除标点符号但保留表情符号?

r - 部署 Shiny 的应用程序时出错

r - 如何处理每次有多个点的时间序列(在 R 中)?

html - 动态加载 html title 属性