在ggplot中,将图例标签添加到手动色标会导致出现两个图例 [英] In ggplot, adding legend labels to manual color scale causes two legends to appear
本文介绍了在ggplot中,将图例标签添加到手动色标会导致出现两个图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用下面的代码,我可以生成所需的图形,但是当我尝试更改scale_color_manual部分中的图例标签时,仅对线型变量显示第二个图例
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
原始代码:
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: 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])))
新剧情:
如何在不生成两个单独的图例的情况下为图例设置标签,一个用于颜色,另一个用于线型?
How can I set the labels for the legend without generating two separate legends, one for the color and another for the linetype?
推荐答案
您需要为 linetype
和 color
设置相同的标签:
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)
这篇关于在ggplot中,将图例标签添加到手动色标会导致出现两个图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文