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

时间:2015-11-05 03:20:18

标签: r ggplot2 ecdf

使用下面的代码,我可以生成我想要的图形,但是当我尝试更改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')) 

简介: enter image description here 修改/添加代码:

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

新剧情:

enter image description here

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

1 个答案:

答案 0 :(得分:1)

您需要为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)

enter image description here