Вот несколько способов добавить заголовок к легенде в ggplot:

  1. Метод 1: использование функции labs()

    library(ggplot2)
    ggplot(data, aes(x = x, y = y, color = category)) +
    geom_point() +
    labs(color = "Legend Title")
  2. Метод 2: изменение функции guide_legend()

    library(ggplot2)
    ggplot(data, aes(x = x, y = y, color = category)) +
    geom_point() +
    guides(color = guide_legend(title = "Legend Title"))
  3. Метод 3: использование функции scale_color_*()

    library(ggplot2)
    ggplot(data, aes(x = x, y = y, color = category)) +
    geom_point() +
    scale_color_discrete(name = "Legend Title")
  4. Метод 4. Изменение функции theme()

    library(ggplot2)
    ggplot(data, aes(x = x, y = y, color = category)) +
    geom_point() +
    theme(legend.title = element_text(text = "Legend Title"))