在图中着色部分标题

时间:2013-06-13 09:14:41

标签: r colors formatting plot

是否可以在图中为部分标题着色?

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

在此图中,我想将slope=1的颜色更改为红色。

3 个答案:

答案 0 :(得分:27)

这是解决您问题的一个非常简单的方法:

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

答案 1 :(得分:2)

使用ggtext包对ggplot2地块的解决方案

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))

reprex package(v0.3.0)于2019-08-11创建

答案 2 :(得分:1)

ggtext package可以做到

enter image description here

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

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(
    name = NULL,
    values = c(setosa = "#0072B2", virginica = "#009E73", versicolor = "#D55E00"),
    labels = c(
      setosa = "<i style='color:#0072B2'>I. setosa</i>",
      virginica = "<i style='color:#009E73'>I. virginica</i>",
      versicolor = "<i style='color:#D55E00'>I. versicolor</i>")
  ) +
  labs(
    title = "**Fisher's *Iris* dataset**  
    <span style='font-size:11pt'>Sepal width vs. sepal length for 
    <span style='color:#0072B2;'>setosa</span>, 
    <span style='color:#D55E00;'>versicolor</span>, and
    <span style='color:#009E73;'>virginica</span>
    </span>",
    x = "Sepal length (cm)", y = "Sepal width (cm)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_markdown(lineheight = 1.1),
    legend.text = element_markdown(size = 11)
  )