ggplot2: color individual words in title to match colors of groups

前端 未结 3 1830
一向
一向 2020-12-07 23:38

I recently saw a line chart in the Economist where the title had colored words to match the colors of the groups used in the line chart. I was wondering how to do this with

3条回答
  •  没有蜡笔的小新
    2020-12-07 23:44

    Here's a simple and more general way using the ggtext package

    produced with:

    library(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. setosa",
          virginica = "I. virginica",
          versicolor = "I. versicolor")
      ) +
      labs(
        title = "**Fisher's *Iris* dataset**  
        Sepal width vs. sepal length for 
        setosa, 
        versicolor, and
        virginica
        ",
        x = "Sepal length (cm)", y = "Sepal width (cm)"
      ) +
      theme_minimal() +
      theme(
        plot.title = element_markdown(lineheight = 1.1),
        legend.text = element_markdown(size = 11)
      )
    

提交回复
热议问题