【问题标题】:Unequal horizontal adjustment to title lines in ggplotggplot中标题行的水平调整不等
【发布时间】:2019-06-19 08:02:16
【问题描述】:

我有一个带有相当宽的 y 轴标签的图,所以我想将标题调整到左侧,使其与标签而不是轴齐平(如 this question)但扭曲的是我有一个多行标题。我一直在使用 hjust,但它以不同的方式调整两条线。 例如

ggplot(mtcars,aes(x=wt,y=mpg))+
  geom_point()+
  ggtitle("Figure: My long and winding title\nthat goes on and on and on") +
  ylab("My long label") +
  theme_bw() +
  theme(plot.title = element_text(size=16, hjust=-.33, color="black", face="bold")) +
  theme(axis.title.y = element_text(angle = 0, hjust = 1))

给予 有没有办法让标题的两行开始水平调整后齐平?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以使用以下代码。首先创建绘图并将其分配给g,然后将g 转换为grobggplotGrob。因此,在 grob 的 layout 部分中操作标题左对齐(从 5 到 2)。最后绘制改编的 grob。

    g <- ggplot(mtcars,aes(x=wt,y=mpg)) + 
      geom_point() + 
      ggtitle("Figure: My long and winding title\nthat goes on and on and on") + 
      ylab("My long label") + 
      theme_bw() + 
      theme(plot.title = element_text(size=16,  color="black", face="bold"))  + 
      theme(axis.title.y = element_text(angle = 0, hjust = 1))
    
    
    grob <- ggplotGrob(g)
    
    # what is the current content
    grob$layout$l[grob$layout$name == "title"]
    [1] 5
    
    grob$layout$l[grob$layout$name == "title"] <- 2
    
    # plot the new grob
    grid::grid.draw(grob)
    

    产生这个情节:

    请告诉我这是否是你想要的。

    【讨论】:

    • 我认为这是最好/最干净的答案。
    【解决方案2】:

    对不起,我误解了你的问题。

    我认为您只是在标题中缺少了一个空格。

    ggtitle("Figure: My long and winding title\n that goes on and on and on")

    【讨论】:

    • 我已经有了一个带有 hjust 的 plot.title 主题。这个新的只是覆盖它。如果我将它设置为原来的 -.33 我会遇到同样的问题
    • 对不起,你为什么希望它是-0.33?上面的输出不正确吗?您希望标题位于 y 轴的左侧吗?
    • 是的,“我想将标题向左调整,使其与标签而不是轴齐平”
    【解决方案3】:

    给你:

    library(ggplot2)
    library(grid)
    library("gridExtra")
    p<-ggplot(mtcars,aes(x=wt,y=mpg))+
      geom_point()+
      ggtitle("") +
      ylab("My long label") +
      theme_bw() +theme(axis.title.y = element_text(angle = 0, hjust = 1))
    
    
    title.grob <- textGrob(
      label = "Figure: My long and winding title\nthat goes on and on and on",
      x = unit(0, "lines"), 
      y = unit(0, "lines"),
      hjust = 0, vjust = 0,
      gp = gpar(fontsize = 16))
    p1 <- arrangeGrob(p, top = title.grob)
    grid.draw(p1)`
    

    【讨论】:

      【解决方案4】:

      hjust 负责空白区域。 删除它会删除第二行上的 2 个空格。

      library(ggplot2)
      
      ggplot(mtcars,aes(x=wt,y=mpg))+
        geom_point()+
        ggtitle("Figure: My long and winding title\nthat goes on and on and on") +
        ylab("My long label") +
        theme_bw() +
        theme(plot.title = element_text(size=16, 
                                        color="black", face="bold",
                                        ),
              axis.title.y = element_text(angle = 0, hjust = 1))
      

      编辑 1:

      如果你想自动将标题分成多行,你可以使用gsub。这是一个示例,我在 30 个字符后拆分标题。 (source)

      long_title <- "Figure: My long and winding title that goes on and on and on"
      
      ggplot(mtcars,aes(x=wt,y=mpg))+
        geom_point()+
        ggtitle(gsub('(.{1,30})(\\s|$)', '\\1\n', long_title)) +
        ylab("My long label") +
        theme_bw() +
        theme(plot.title = element_text(size=16, 
                                        color="black", face="bold",
                                        ),
              axis.title.y = element_text(angle = 0, hjust = 1))
      

      希望有所帮助!

      【讨论】:

      • 但是我想水平调整标题。两行一样。
      • @pgcudahy 很抱歉造成误解。 EDIT1 根据每行字符数(此处为 30)分割长标题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 2021-05-02
      • 2023-03-27
      • 2022-01-18
      相关资源
      最近更新 更多