You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用cowplot包add_sub函数时如何对齐多行文本标签

解决cowplot::add_sub多行标注对齐问题

问题根因

  • 你写的标注文本中,\n换行符后手动添加了大量缩进空格,这些空格会被识别为文本内容直接渲染,导致第二行整体向右偏移
  • 没有显式指定对齐参数hjust,add_sub默认值为0.5(居中对齐),不符合自定义对齐的需求

修正方案

先删掉文本中换行后的多余缩进空格,再根据对齐需求添加hjust参数即可:

library(ggplot2)
library(cowplot)
p1 <- ggplot(mtcars,aes(mpg,disp))+
  geom_line(colour="blue")+
  theme_half_open()

# hjust=0对应左对齐,0.5对应居中,1对应右对齐,可根据需求调整
p1_1 <- add_sub(p1,"this is an annotation.\nAnnotations can span multiple lines.",
                color="red",size=14,
                vpadding = grid::unit(0.5,"lines"),
                lineheight = 0.5,
                x=0.3,y=0.4,
                hjust = 0)
ggdraw(p1_1)

内容的提问来源于stack exchange,提问作者anderwyang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 03:36:05