1
votes

Using the code below I can generate the graph I want, but when I try to change the legend labels in the scale_color_manual section, a second legend appears for only the linetype variable

Original code:

set.seed(124)
DF <- data.frame(values=rnorm(1000,500,200),type=sample(LETTERS[1:5],1000,T))

ggplot(DF, aes(x=values, color=type, linetype=type)) + 
  stat_ecdf(size=1, n=750, show_guide=T) + 
  xlab(expression('Index Value')) + 
  ylab("Cumulative Density") +
  ggtitle(expression('All Index Values')) +
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0)) +
  theme(text = element_text(size=20),
        plot.title = element_text(size=30,face="bold",vjust=1),
        axis.title.x=element_text(size=20,face="bold",vjust=0,hjust=0.5),
        axis.title.y=element_text(size=20,face="bold",vjust=1.0,hjust=0.5),
        legend.position = c(0.85, 0.25),
        legend.text.align = 0,
        legend.box = 'horizontal',
        legend.margin = unit(45.0, 'line'),
        legend.text=element_text(size=28,vjust=0,hjust=0),
        legend.title=element_blank(),
        legend.key=element_blank(),
        legend.key.height = unit(1.5, 'line'),
        legend.key.width = unit(1.5, 'line'),
        panel.background = element_rect(fill = "white")) + 
  scale_color_manual(values=c('grey25','grey35','grey45','grey55','grey65')) 

Plot: enter image description here Modified/added code:

  scale_color_manual(values=c('grey25','grey35','grey45','grey55','grey65'),
                     labels=c(expression(TI[c]),expression(TI[p]),
                              expression(TI[d]),expression(TI[l]),
                              expression(TI[w])))

New Plot:

enter image description here

How can I set the labels for the legend without generating two separate legends, one for the color and another for the linetype?

1

1 Answers

1
votes

You need to set the same labels for both linetype and color:

  labs = c(expression(TI[c]),expression(TI[p]), expression(TI[d]),expression(TI[l]),
           expression(TI[w]))

  scale_color_manual(values=c('grey25','grey35','grey45','grey55','grey65'), labels=labs) +
  scale_linetype_manual(values=1:5, labels=labs)

enter image description here