无法获取stat_ecdf图例线条颜色以匹配绘图颜色和删除水平线条

时间:2015-11-02 07:21:12

标签: r plot ggplot2 legend

我有一个包含三个ECDF和三条垂直线的图,并且无法获得stat_ecdf图例线颜色以匹配实际线条颜色。

以下是ECDF情节的代码:

Integer

这是情节:

enter image description here

但我无法弄清楚如何以我想要添加的方式添加我想要添加的垂直线:

代码:

set.seed(124)
allDTI <- data.frame(values=rnorm(1000,500,200),type=sample(LETTERS[1:3],1000,T))
meanALLDTI <- ddply(allDTI, "type", summarise, values.mean=mean(values, na.rm=TRUE), n)


ggplot(allDTI, aes(x=values, color=type)) + 
  stat_ecdf(size=1, show_guide=T) + 
  xlab(expression('Index Value')) + 
  ylab("Cumulative Density") +
  ggtitle(expression(TI[d]~'and'~TI[l]~'and'~TI[w])) +
  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.key.height = unit(1.5, 'line'),
        legend.key.width = unit(1.5, 'line'),
        panel.background = element_rect(fill = "white")) + 
  scale_color_manual(values=c('grey10','grey30','grey50'),
                     labels=c(expression(TI[d]),expression(TI[l]),
                              expression(TI[w]))) +
  guides(color = guide_legend(title=NULL))

简介: enter image description here

作为我想要的一个例子,以下是密度图的结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

经过一些反复试验后,我认为最好使用geom_segment来获得一个不错的传奇。用:

ggplot(allDTI, aes(x=values, color=type)) + 
  stat_ecdf(size=1) +
  geom_segment(data=meanALLDTI, aes(x=values.mean, xend=values.mean, y=0, yend=1, color=type, linetype = type), size=1) +
  labs(title=expression(TI[d]~'and'~TI[l]~'and'~TI[w]), x="Index Value", y="Cumulative Density") +
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0)) +
  scale_color_manual(values=c("grey10", "grey30", "grey50"),
                     labels=c(expression(TI[d]),expression(TI[l]),expression(TI[w]))) +
  scale_linetype_manual(values=c(3,2,5),
                        labels=c(expression(TI[d]),expression(TI[l]),expression(TI[w]))) +
  guides(color = guide_legend(title="color",
                              override.aes = list(color = c("grey10", "grey30", "grey50"),
                                                  size = 2)),
         linetype = guide_legend(title="lty", override.aes = list(linetype = c(3,2,5),
                                                                   size = 0.7))) +
  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.box = 'horizontal',
        legend.title = element_blank(),
        legend.text.align = 0,
        legend.text = element_text(size=20,vjust=0,hjust=0),
        legend.key = element_rect(fill=NA),
        legend.key.height = unit(1.5, 'line'),
        legend.key.width = unit(1.5, 'line'),
        panel.background = element_rect(fill = "white"))

你得到:

enter image description here