使用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
相关产品推荐
相关产品推荐

