0

I have a plot that looks like this:

library('ggplot2')

df = data.frame(Sepal.Width = 5.6, Sepal.Length = 3.9) 

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
  geom_point() +
  geom_point(data = df, col = 'blue')

I want to label point blue in the legend. How do I do that?

MAPK
  • 5,635
  • 4
  • 37
  • 88
  • 3
    I'm not sure if I understand what you want to do. However, if you would like that that last geom_point to be something represented in the legend, then change it to `geom_point(data = df, aes(col = 'blue'))` The color won't be blue anymore, but the label is "blue." – Kat Dec 29 '21 at 19:35

1 Answers1

3

One way is to assign a color with some name in aes for the point. Then, you can change the color and name manually in scale_color_manual.

library(tidyverse)

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
  geom_point() +
  geom_point(aes(x = Sepal.Width, y = Sepal.Length, colour = 'Sepal.Width'),
             data = df) +
  scale_color_manual(
    values = c(
      "Sepal.Width" = "blue",
      "setosa" = "red",
      "versicolor" = "darkgreen",
      "virginica" = "orange"
    ),
    labels = c('New Point', "setosa", "versicolor", "virginica")
  )

Output

enter image description here

AndrewGB
  • 16,126
  • 5
  • 18
  • 49