Colorize parts of the title in a plot

2020-02-03 05:09发布

Is it possible to colorize parts of the title in a plot?

x = 1:10
y = 1:10
plot(x, y, main="title (slope=1)")

In this plot I'd like to change the color of slope=1 to red.

2条回答
2楼-- · 2020-02-03 06:00

A solution for ggplot2 plots using the ggtext package

library(ggplot2)
# devtools::install_github("clauswilke/ggtext")
library(ggtext)

p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + 
  geom_point(size = 3)

p + 
  labs(title = "New plot <b style='color:#009E73'>title</b>", 
       subtitle = "A <b style='color:#D55E00'>subtitle</b>") +
  theme_classic(base_size = 24) +
  theme(plot.title = element_markdown(lineheight = 1.1),
        plot.subtitle = element_markdown(lineheight = 1.1))

Created on 2019-08-11 by the reprex package (v0.3.0)

查看更多
▲ chillily
3楼-- · 2020-02-03 06:09

This is a quite simple solution to your problem:

plot(x, y)
title(expression("title (" * phantom("slope=1)") * ")"), col.main = "black")
title(expression(phantom("title (") * "slope=1"), col.main = "red")

enter image description here

查看更多
登录 后发表回答