这个问题在这里已经有了答案:
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)
)
不良情节可取的情节
最佳答案
我同意@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)
您可以更改
4.5
所需的效果。
关于r - 如何对齐多行标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65552555/