首页 文章

ggplot2多行 Headers ,不同的缩进

提问于
浏览
4

我正在为出版物生成图表,我希望能够在ggplot本身标注图形的面板(而不是导出到出版商等),以便它们在最终文档中整齐地整合在一起 . 我打算通过在 Headers 中添加一个字母(“A”)来尝试这样做,但我希望我的 Headers 居中,我想要左上角的字母 .

# base graph:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
  geom_jitter(size = 6.5)+
  ggtitle("A \n \n The Actual Long, Normal Title of Titliness")+
  theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 30),
        axis.ticks = element_blank(),
        legend.text = element_text(size = 25),
        axis.title = element_text(size = 25, face = "bold"),
        axis.text = element_text(size = 25, vjust = 0.05),
        legend.position = "bottom")

oh noes! both of them are centered over the graph

现在,如果我愿意通过手工间隔每个 Headers “伪造它”,我可以让它起作用,但这似乎是时间密集和粗糙的 .

# sloppy solution
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
  geom_jitter(size = 6.5)+
  ggtitle("A \n \n             The Actual Long, Normal Title of Titliness")+
  theme(plot.title = element_text(hjust = 0,face = "bold", size = 30),
        axis.ticks = element_blank(),
        legend.text = element_text(size = 25),
        axis.title = element_text(size = 25, face = "bold"),
        axis.text = element_text(size = 25, vjust = 0.05),
        legend.position = "bottom")

better spacing but sloppy code

有没有办法单独调用 Headers 的每个“行”以获得自己的hjust值?

还有其他创意解决方案

另外,我在mtext(Splitting axis labels with expressions)中看到了潜力,但却无法兼容) . 这篇文章各种各样有趣(Multi-line ggplot Title With Different Font Size, Face, etc),但我还是R的新手,我无法弄清楚如何编辑这个聪明的东西来改变 indentation .

谢谢!

5 回答

  • 1

    Update: Since ggplot2 3.0.0 there is now native support for plot labels, see this answer.

    以下是我将如何使用我专门为此目的编写的cowplot包 . 请注意,您获得了一个干净的主题作为奖励 .

    library(cowplot)
    
    p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
      geom_jitter(size = 3.5) +
      ggtitle("The Actual Long, Normal Title of Titliness")
    
    # add one label to one plot
    ggdraw(p) + draw_plot_label("A")
    

    enter image description here

    # place multiple plots into a grid, with labels
    plot_grid(p, p, p, p, labels = "AUTO")
    

    enter image description here

    然后,您希望使用 save_plot 函数来保存绘图而不是 ggsave ,因为 save_plot 具有默认值和参数,可帮助您获得相对于主题的缩放,特别是对于网格中的绘图 .

  • 6

    ggplot2 3.0.0there is a native way to do this, using the tag label

    ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
      geom_point() +
      labs(title = "The Actual Long, Normal Title of Titliness",
           tag = "A")
    

    ggplot_tag

  • 2

    最简单的方法可能是使用 Headers 和副 Headers ,

    ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
      geom_jitter(size = 6.5) +
      labs(title = "A", subtitle = "The Actual Long, Normal Title of Titliness") +
      theme(plot.subtitle = element_text(hjust = 0.5))
    
  • 3

    你可以通过操纵Grobs来做到这一点 . 在 ggplot2 之上使用 gtablegrid .

    library("ggplot2")
    library("gtable")
    library("grid")
    
    p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
      geom_jitter(size = 6.5)+
      ggtitle("The Actual Long, Normal Title of Titliness")+
      theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 30),
            axis.ticks = element_blank(),
            legend.text = element_text(size = 25),
            axis.title = element_text(size = 25, face = "bold"),
            axis.text = element_text(size = 25, vjust = 0.05),
            legend.position = "bottom",
            plot.margin = unit(c(4, 1, 1, 4), "lines"))  # We need bigger top/left margins to show the letter
    
    gt <- ggplot_gtable(ggplot_build(p))  # Building a gtable from the ggplot object
    # Adding the textGrob for the panel letter
    panel_letter = textGrob("A", x = 0.5, y = .9, 
                            just = c("left", "top"), 
                            gp = gpar(fontsize = 40, col =  "black", face="bold"))
    gt <- gtable_add_grob(gt, panel_letter, t=1, l=1, r=1)  # Append the Grob to the table
    
    p <- grid.draw(gt)  # Display the plot
    ggsave("plot.png",gt, width=10, height=10)  # If you want to save it.
    

    My attempt

  • 7

    您可以使用包 grid 中的 grid.text ,在绘图中添加文本 . 首先在 Headers 上方添加一些空格 plot.margin .

    library(grid)
    p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
    geom_jitter(size = 6.5)+
    ggtitle("The Actual Long, Normal Title of Titliness")+
    theme(plot.title = element_text(hjust = 0,face = "bold", size = 30),
          axis.ticks = element_blank(),
          legend.text = element_text(size = 25),
          axis.title = element_text(size = 25, face = "bold"),
          axis.text = element_text(size = 25, vjust = 0.05),
          legend.position = "bottom", 
          plot.margin = unit(c(4, 3, 1, 1), "lines"))
    p
    grid.text('A', x = unit(0.1, 'npc'), y = unit(.90, 'npc'), gp = gpar(fontsize=28))
    

    enter image description here

相关问题