简体   繁体   English

ggplot结合线型和填充图例

[英]ggplot combining linetype and fill in legend

Do you how the way to combine linetype and fill in legend??请问如何结合线型和填充图例??

Here is my dataset:这是我的数据集:

values <- runif(1200, 1, 100)
ind <- as.factor(rep(c(1:6), each=200))
inout <- as.factor(rep(c(1:2), each =600))
df <- data.frame(values,ind,inout)

ggplot(df) + 
geom_density(aes(x=values, y=..density..*100, group=interaction(ind,inout), linetype=factor(inout), colour=ind), size =1, alpha=1,na.rm = TRUE) +
geom_density(aes(x=values, y=..density..*100, group=inout, linetype=factor(inout), fill=factor(inout)), alpha=.4)

The original plot:原剧情:

在此处输入图片说明

I would like to combine the legend "factor(inout)" and legend "NA".我想结合图例“因子(输入输出)”和图例“NA”。

Thanks for your help.谢谢你的帮助。

Use manual scales and make sure they both have identical names and labels (similar idea here ):使用手动秤并确保它们都具有相同的名称和标签( 这里的想法类似):

ggplot(df, aes(x=values, y=..density..*100, linetype=factor(inout))) + 
  geom_density(aes(group=interaction(ind, inout), colour=ind), 
               size=1, alpha=1, na.rm=TRUE) +
  geom_density(aes(group=inout, fill=factor(inout)), alpha=.4) + 
    scale_fill_manual(name = "fancy curves", labels = 1:2, values = c("red", "blue")) + 
    scale_linetype_manual(name = "fancy curves", labels = 1:2, values = 1:2)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM