How to create an legend for ggplot for just one series of data and then add legend for additional horizontal lines?

Viewed 29

I'm wanting to prepare a simple plot with some points and horizontal lines with a legend. The code below generates the desired plot and a legend but the legend symbols are combinations of the shape and line, when I would just like a shape for the shape and a line for the line.

dat <- iris %>% select(Sepal.Length)
dat$Type <- "Sepal.Length"

ggplot() +
  geom_point(data = dat, aes(x = as.numeric(row.names(dat)), y = Sepal.Length, colour = Type), shape = 10, size = 2) +
  geom_hline(aes(yintercept = 6, colour = "Some line"), linetype = "dashed")

Example plot

1 Answers

Custom linetypes and shapes are assigned using scale_*_manual, like so:

dat %>% 
  ggplot() +
  geom_point(aes(x = as.numeric(row.names(dat)), y = Sepal.Length, shape = Type), size = 2) +
  geom_hline(aes(yintercept = 6, linetype = 'Some line')) +
  scale_linetype_manual(values = c('Some line' = 'dashed')) +
  scale_shape_manual(values = c('Sepal.Length' = 10))

enter image description here

Related