MLEN
MLEN

Reputation: 2561

Changing labels for linetype in ggplot2

How can I change the legend title to Group and the values One and Two?

library(ggplot2)

df <- data.frame(date = c('2018-01-01', '2019-01-01', '2018-01-01', '2019-01-01'),
                 val  = 1:4,
                 gr   = c('1', '1', '2', '2'))

ggplot(df, aes(x = date, y = val, linetype = gr, group = gr)) + 
  geom_line() +
  scale_linetype_manual(name = 'Group', labels = c('one', 'two'))

Upvotes: 0

Views: 34

Answers (1)

stefan
stefan

Reputation: 124393

Use scale_linetype_discrete instead of scale_linetype_manual:

df <- data.frame(date = c('2018-01-01', '2019-01-01', '2018-01-01', '2019-01-01'),
                 val  = 1:4,
                 gr   = c('1', '1', '2', '2'))

library(ggplot2)

ggplot(df, aes(x = date, y = val, linetype = gr, group = gr)) + 
  geom_line() +
  scale_linetype_discrete(name = 'Group', labels = c(`1` = 'one', `2` = 'two'))

Created on 2020-06-22 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions