r - 使用 ggtext 的 ggplots 拼凑标题中的颜色?

标签 r ggplot2 patchwork ggtext

如何使用 ggtext 为拼凑而成的 ggplots 赋予彩色标题?

例子

假设我们有四个图

library(ggplot2)
library(patchwork)
library(ggtext)

p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

p4 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle('Plot 4')

这些可以这样安排
patch <- (p1 + p2) / (p3 + p4)
patch

enter image description here

拼凑而成的标题可以像这样
patch +
  plot_annotation(
  title = "Here is a regular title")

enter image description here

单个 ggplot 可以像这样被赋予一个丰富多彩的标题
p1 +
  ggtitle("Here <span style='color:#953011;'><strong>is a colourful title</strong></span>") +
  theme(plot.title = element_markdown(lineheight = 1.1)) 

enter image description here

ggplots的拼凑如何才能获得一个丰富多彩的标题。这是我失败的尝试
patch +
  plot_annotation(
  title = "Here<span style='color:#953011;'><strong>is a colourful title</strong></span>") +
  theme(plot.title = element_markdown(lineheight = 1.1)) 

enter image description here

最佳答案

plot_annotation有一个 theme论证,所以你可以做

#remotes::install_github("wilkelab/ggtext")
library(ggplot2)
library(patchwork)
library(ggtext)

patch <- (p1 + p2)

patch +
  plot_annotation(
    title = "Here <span style='color:#953011;'><strong>is a colourful title</strong></span>",
    theme = theme(plot.title = element_markdown(lineheight = 1.1)))

enter image description here

关于r - 使用 ggtext 的 ggplots 拼凑标题中的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61642489/

相关文章:

r - 将表达式与 sprintf 结合起来,在 R 中生成用于 ggplot/plot-labeling 的结果

r - 使用 tidyr 分隔具有多个不同条目的列

r - 在R中按组对所有行重复检查

r - 连接没有重复的数据框 r

r - 如何为具有多个面板的组合图创建公共(public)/共享/中心 y 轴标题?

r - 使用 R 中的 ggplot 和 patchwork 更改轴标签和比例

r - 在 ggraph/ggplot2 中将几何标签放置在圆形布局之外

r - 在水平 geom_bars 上方添加轴文本;使文本左对齐

r - 展开 ggplot2 中拥挤的 x 轴

r - ggplot2,拼凑 - Axis 标题的一部分被相邻图的边缘遮挡