在ggplot中,将图例标签添加到手动色标会导致出现两个图例

In ggplot, adding legend labels to manual color scale causes two legends to appear

使用下面的代码我可以生成我想要的图表,但是当我尝试更改 scale_color_manual 部分中的图例标签时,第二个图例仅针对线型变量出现

原代码:

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')) 

剧情: Modified/added代码:

  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])))

新剧情:

如何设置图例的标签而不生成两个单独的图例,一个用于颜色,另一个用于线型?

您需要为 linetypecolor 设置相同的标签:

  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)