【问题标题】:How can a title with subtitle be added with annotate_figure?annotate_figure如何添加带字幕的标题?
【发布时间】:2019-09-30 01:06:46
【问题描述】:

我正在使用ggarrangeannotate_figure 并排显示两个图。在我的实际代码中,我在每个子图上都有不同的标题,但是在整个图的顶部需要一个共同的标题和副标题。

我了解如何在顶部添加文本,但是如何添加两行,每行具有不同的字体(例如,顶部标题应该是大/粗体,第二行/副标题应该是较小的常规字体)?

这是我制作标题的方式。有没有办法将text_grob 添加到top

library(tidyverse)
library(ggpubr)

df <- data.frame()
plot1 <- ggplot(df) + geom_point() + xlim(0, 10) + ylim(0, 100)
plot2 <- ggplot(df) + geom_point() + xlim(0, 10) + ylim(0, 100)

all.plots <- ggarrange(plot1, plot2)
annotate_figure(all.plots,
                top=text_grob("Antibiotic Effectiveness"))

【问题讨论】:

标签: r ggplot2 gridextra ggpubr


【解决方案1】:

您可以使用 plotmath 为 text_grob 创建表达式。请参阅 ?plotmath。

library(tidyverse)
library(ggpubr)

df <- data.frame(x=runif(10,0,10), y=runif(10,0,100))
plot1 <- ggplot(df) + geom_point(aes(x=x, y=y)) + xlim(0, 10) + ylim(0, 100)
plot2 <- ggplot(df) + geom_point(aes(x=x, y=y)) + xlim(0, 10) + ylim(0, 100)

all.plots <- ggarrange(plot1, plot2)
# construct plotmath expression
title <- expression(atop(bold("Figure 1:"), scriptstyle("This is the caption")))
annotate_figure(all.plots,
                top=text_grob(title))

reprex package (v0.3.0) 于 2019 年 10 月 2 日创建

【讨论】:

    猜你喜欢
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多