【问题标题】:How to center ggplot plot title如何使ggplot图标题居中
【发布时间】:2018-02-27 10:49:06
【问题描述】:

在 ggplot - plot.title = element_text(hjust = 0.5) 中居中对齐绘图标题的“lege artis”方式 - 将标题居中于绘图区域不包括轴标签

当轴标签很长时,这可能会变得很难看,例如 Mary Poppins Soundtrack 中的歌曲与字符长度的关系。

library(tidyverse)

mary_poppins <- data_frame(song = c("Overture", "Sister Suffragette", "The Life I Lead", "The Perfect Nanny", "A Spoonful of Sugar", "Pavement Artist", "Jolly Holiday", "Supercalifragilisticexpialidocious", "Stay Awake", "I Love to Laugh", "A British Bank", "Feed the Birds ", "Fidelity Fiduciary Bank", "Chim Chim Cher-ee", "Step in Time", "A Man Has Dreams", "Let's Go Fly a Kite"
))

mary_poppins <- mary_poppins %>%
  mutate(len = nchar(song))

ggplot(data = mary_poppins, aes(x = reorder(song, len), y = len)) +
  geom_col(fill = "firebrick") +
  coord_flip() +
  theme_light() +
  theme(axis.title.y = element_blank(),
        axis.text = element_text(size = rel(1.5)),
        plot.title = element_text(size = rel(2.5), face = "bold", hjust = 0.5, 
                                  margin = margin(t = 10, b = 20, unit = "pt"))) +
  ggtitle("Mary Poppins") +
  ylab("Lenght of title (characters)")

有没有办法让标题在总绘图区域上居中,即包括轴标签所占据的区域?

【问题讨论】:

    标签: r ggplot2 data-visualization


    【解决方案1】:

    在中心标题添加空格的解决方案:

    在标题后添加空格:

    ggtitle(paste0("Mary Poppins", paste0(rep("", 30), collapse = " ")))
    

    对于这样的输出:

    不是完美的解决方案,但有效。

    【讨论】:

      【解决方案2】:

      或者,您可以使用gridExtra::grid.arrangegrid::textGrob 创建标题,而无需在视觉上进行填充。这基本上创建了一个带有您的标题的单独绘图对象并将其粘贴在顶部,与您的 ggplot 调用的内容无关。

      首先将整个ggplot 调用存储在一个变量中,例如p1:

      grid.arrange(textGrob("Mary Poppins", 
                     gp = gpar(fontsize = 2.5*11, fontface = "bold")), 
                   p1, 
                   heights = c(0.1, 1))
      

      您必须将您的theme() 设置转换为gpar()theme_light 的基本大小是 11,这是 2.5*11 的来源(以及您的 rel(2.5) 的 2.5)。

      这里的好处是您知道您的标题将真正居中,而不仅仅是靠肉眼足够近。

      【讨论】:

      • 谢谢 - 这确实是一个比填充空格更简洁的解决方案!
      【解决方案3】:

      如果你赶时间,在 ggtitle 中的 title 后添加空格也可以...

      ggtitle("Mary Poppins                                 ") +
      

      输出:

      【讨论】:

        【解决方案4】:

        我找到的短解决方案:

        theme(plot.title = element_text(hjust = -0.2))
        

        hjust 参数控制从左对齐到 y 轴的距离。负值将文本向左移动

        【讨论】:

          【解决方案5】:

          正如我在基本的duplicate question 中回复的那样,您可以使用patchwork 库,它是plot_annotation,所以您将拥有:

          library(tidyverse)
          
          mary_poppins <- data_frame(song = c("Overture", "Sister Suffragette", "The Life I Lead", "The Perfect Nanny", "A Spoonful of Sugar", "Pavement Artist", "Jolly Holiday", "Supercalifragilisticexpialidocious", "Stay Awake", "I Love to Laugh", "A British Bank", "Feed the Birds ", "Fidelity Fiduciary Bank", "Chim Chim Cher-ee", "Step in Time", "A Man Has Dreams", "Let's Go Fly a Kite"
          ))
          mary_poppins <- mary_poppins %>%
            mutate(len = nchar(song))
          
          ggplot(data = mary_poppins, aes(x = reorder(song, len), y = len)) +
            geom_col(fill = "firebrick") +
            coord_flip() +
            theme_light() +
            theme(axis.title.y = element_blank(),
                  axis.text = element_text(size = rel(1.5))) +
            patchwork::plot_annotation("Mary Poppins", 
                  theme = theme(plot.title = element_text(size = rel(2.5), face = "bold", hjust = 0.5, 
                                            margin = margin(t = 10, b = 20, unit = "pt")))) +
            ylab("Lenght of title (characters)")
          

          由于plot_annotation是为多面板地块设计的,它不会继承你正在构建的地块的主题,所以你需要单独给它。但是,它将尊重任何全球主题。

          结果图(希望)是您所需要的。

          【讨论】:

          • 不是典型的 {patchwork} 用例,但它确实有效 - 感谢分享!
          【解决方案6】:

          解决问题的一个简单方法是在绘图中添加以下内容:

          theme(plot.title.position = 'plot', 
                plot.title = element_text(hjust = 0.5))
          

          第一部分告诉ggplot 使用整个绘图作为居中的参考,第二部分让标题居中。

          【讨论】:

            猜你喜欢
            • 2013-12-07
            • 2021-12-27
            • 2022-11-02
            • 1970-01-01
            • 2018-11-25
            相关资源
            最近更新 更多