Полное руководство: как изменить название легенды в R

library(ggplot2)
# Create a scatter plot with custom legend title
ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
  geom_point() +
  labs(color = "Custom Legend Title")

Метод 2: изменение заголовка легенды на графиках базы R
Для графиков базы R вы можете изменить заголовок легенды, изменив графические параметры. Используйте функцию legend()в сочетании с параметром title. Вот пример:

# Create a scatter plot with custom legend title
plot(x = iris$Sepal.Width, y = iris$Sepal.Length, col = iris$Species)
legend("topright", legend = levels(iris$Species), col = 1:3, pch = 1:3,
       title = "Custom Legend Title")
library(ggplot2)
# Create a scatter plot with custom legend title
ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
  geom_point() +
  scale_color_discrete(name = "Custom Legend Title")

Метод 4: изменение заголовка легенды на решетчатых графиках
Для решетчатых диаграмм вы можете изменить заголовок легенды с помощью параметра keyфункции xyplot(). Вот пример:

library(lattice)
# Create a scatter plot with custom legend title
xyplot(Sepal.Length ~ Sepal.Width, data = iris, groups = Species,
       key = list(title = "Custom Legend Title"))

Метод 5: изменение заголовка легенды в графикахplotly
Если вы работаете с интерактивными графиками с помощью пакетаplotly, вы можете изменить заголовок легенды с помощью функции layout(). Вот пример:

library(plotly)
# Create a scatter plot with custom legend title
p <- plot_ly(data = iris, x = ~Sepal.Width, y = ~Sepal.Length, color = ~Species, type = "scatter", mode = "markers")
p <- layout(p, title = "Custom Legend Title")
p