在cowplot/add_sub中,如何对齐多个文本标签?

In cowplot/add_sub, how to align the multiple text label?

cowplot/add_sub中,如何对齐多个文本标签?这是下面的代码。谢谢!

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

p1_1 <- add_sub(p1,"this is an annotation.\n
                Annotations can span multiple lines.",
                color="red",size=14,
                vpadding = grid::unit(0.5,"lines"),
                lineheight = 0.5,
                x=0.3,y=0.4)
ggdraw(p1_1)

虽然第二行在注释之前有一个额外的 space 存在问题,但是这会变得相当接近。

p1_1 <- add_sub(p1,str_replace_all("this is an annotation.\n
              Annotations can span multiple lines.", "\h+", " "),
                color="red",size=14,
                vpadding = grid::unit(0.5,"lines"),
                lineheight = 0.5,
                x=0,
                y=0.4,
                hjust = 0)

ggdraw(p1_1)

不过,如果您已经包括换行符,那么您可以将文本放在一起。

p1_1 <- add_sub(p1,"this is an annotation.\n\nAnnotations can span multiple lines.",
                color="red",size=14,
                vpadding = grid::unit(0.5,"lines"),
                lineheight = 0.5,
                x=0,
                y=0.4,
                hjust = 0)

ggdraw(p1_1)